浏览代码

fixed warnings, added large bytes support for embedded files

Nicolas Cannasse 7 年之前
父节点
当前提交
48edb9535a
共有 1 个文件被更改,包括 30 次插入11 次删除
  1. 30 11
      src/generators/hl2c.ml

+ 30 - 11
src/generators/hl2c.ml

@@ -654,11 +654,11 @@ let generate_function ctx f =
 				(try
 					let fid = PMap.find "__compare" oa.pfunctions in
 					if op = CEq then
-						sexpr "if( %s == %s || (%s && %s && %s(%s,%s) == 0) ) goto %s" (reg a) (reg b) (reg a) (reg b) (funname fid) (reg a) (reg b) (label d)
+						sexpr "if( %s == %s || (%s && %s && %s(%s,(vdynamic*)%s) == 0) ) goto %s" (reg a) (reg b) (reg a) (reg b) (funname fid) (reg a) (reg b) (label d)
 					else if op = CNeq then
-						sexpr "if( %s != %s && (!%s || !%s || %s(%s,%s) != 0) ) goto %s" (reg a) (reg b) (reg a) (reg b) (funname fid) (reg a) (reg b) (label d)
+						sexpr "if( %s != %s && (!%s || !%s || %s(%s,(vdynamic*)%s) != 0) ) goto %s" (reg a) (reg b) (reg a) (reg b) (funname fid) (reg a) (reg b) (label d)
 					else
-						sexpr "if( %s && %s && %s(%s,%s) %s 0 ) goto %s" (reg a) (reg b) (funname fid) (reg a) (reg b) (s_comp op) (label d)
+						sexpr "if( %s && %s && %s(%s,(vdynamic*)%s) %s 0 ) goto %s" (reg a) (reg b) (funname fid) (reg a) (reg b) (s_comp op) (label d)
 				with Not_found ->
 					phys_compare())
 			| HVirtual _, HVirtual _ ->
@@ -1190,17 +1190,36 @@ let write_c com file (code:code) =
 	line "}";
 
 	Array.iteri (fun i str ->
-		let rec loop s i =
-			if i = String.length s then [] else
-			let c = String.get s i in
-			string_of_int (int_of_char c) :: loop s (i+1)
+		let output_bytes f str =
+			for i = 0 to String.length str - 1 do
+				if (i+1) mod 0x80 = 0 then f "\\\n\t";
+				if i > 0 then f ",";
+				f (string_of_int (int_of_char str.[i]));
+			done
 		in
-		if Hashtbl.mem bytes_strings i then
-			sexpr "vbyte bytes$%d[] = {%s}" i (String.concat "," (loop str 0))
-		else if String.length str >= string_data_limit then
+		if Hashtbl.mem bytes_strings i then begin
+			if String.length str > 1000 then begin
+				let bytes_file = "hl/bytes_" ^ (Digest.to_hex (Digest.string str)) ^ ".h" in
+				let abs_file = ctx.dir ^ "/" ^ bytes_file in
+				if not (Sys.file_exists abs_file) then begin
+					let ch = open_out_bin abs_file in
+					output_bytes (output_string ch) str;
+					close_out ch;
+				end;
+				sline "vbyte bytes$%d[] = {" i;
+				output ctx (Printf.sprintf "#%s  include \"%s\"\n" ctx.tabs bytes_file);
+				sexpr "}";
+			end else begin
+				output ctx (Printf.sprintf "vbyte bytes$%d[] = {" i);
+				output_bytes (output ctx) str;
+				sexpr "}";
+			end
+		end else if String.length str >= string_data_limit then
 			let s = utf8_to_utf16 str in
 			sline "// %s..." (String.escaped (String.sub str 0 (string_data_limit-4)));
-			sexpr "vbyte string$%d[] = {%s}" i (String.concat "," (loop s 0))
+			output ctx "vbyte string$%d[] = {" i;
+			output_bytes (output ctx) str;
+			sexpr "}"
 	) code.strings;
 
 	Hashtbl.iter (fun fid _ ->