ソースを参照

Improved the frozen script capabilities, now zipped.

mingodad 13 年 前
コミット
6861aa90e2
2 ファイル変更99 行追加19 行削除
  1. 24 15
      SquiLu-ourbiz/squilufreeze.nut
  2. 75 4
      SquiLu/sq/sq.c

+ 24 - 15
SquiLu-ourbiz/squilufreeze.nut

@@ -15,10 +15,14 @@
 
 // simple test to see if the file exits or not
 function FileExists(filename){
-    local fd = file(filename, "r");
-    if (fd == null) return false;
-    fd.close(file);
-    return true;
+	try {
+		local fd = file(filename, "r");
+		fd.close();
+		return true;
+	}
+	catch(e){
+		return false;
+	}
 }
 
 //////////////////////////////////////////////////////////////////////////////-
@@ -40,15 +44,15 @@ function main(){
     }
 
     local in_squiluFreeze  = vargv[0];
-    local in_luafltkscript  = vargv[1];
+    local in_squiluScript  = vargv[1];
     local out_squiluFreeze = vargv[2];
 
     if (!FileExists(in_squiluFreeze)) {
         printf("Input squiluFreeze program '%s' does not exist! Aborting.\n", in_squiluFreeze);
         return;
     }
-    if (!FileExists(in_luafltkscript)){
-        printf("Input luafltk script '%s' does not exist! Aborting.\n", in_luafltkscript);
+    if (!FileExists(in_squiluScript)){
+        printf("Input luafltk script '%s' does not exist! Aborting.\n", in_squiluScript);
         return;
     }
     if (FileExists(out_squiluFreeze)){
@@ -56,7 +60,7 @@ function main(){
     }
 
     printf("Appending '%s' to squiluFreeze program '%s' and outputting to '%s'\n",
-            in_luafltkscript, in_squiluFreeze, out_squiluFreeze);
+            in_squiluScript, in_squiluFreeze, out_squiluFreeze);
 
     // read the whole squiluFreeze program into memory
     local in_squiluFreeze_handle = file(in_squiluFreeze, "rb");
@@ -65,23 +69,28 @@ function main(){
     in_squiluFreeze_handle.close();
 
     // check to see if they're trying to append another script to squiluFreeze
-    if (in_squiluFreeze_data.sub(in_squiluFreeze_length-21, in_squiluFreeze_length-10) == "squiluFreeze:"){
+    if (in_squiluFreeze_data.slice(in_squiluFreeze_length-21, in_squiluFreeze_length-10) == "squiluFreeze:"){
         printf("Input squiluFreeze program '%s' already has a squilu script attached! Aborting.\n", in_squiluFreeze);
         return;
     }
 
     // read the whole luafltk script into memory
-    local in_luafltkscript_handle = file(in_luafltkscript, "rb")
-    local in_luafltkscript_length = in_luafltkscript_handle.len();
-    local in_luafltkscript_data   = in_luafltkscript_handle.read(in_luafltkscript_length);
-    in_luafltkscript_handle.close()
+    local in_squiluScript_handle = file(in_squiluScript, "rb")
+    local in_squiluScript_length = in_squiluScript_handle.len();
+    local in_squiluScript_data   = in_squiluScript_handle.read(in_squiluScript_length);
+    in_squiluScript_handle.close()
+    
+    local zipedScript = zlib.deflate(in_squiluScript_data);
+    print("zipedScript", in_squiluScript_length, zipedScript.len());
+    in_squiluScript_length = zipedScript.len();
+    in_squiluScript_data = zipedScript;
 
     // write the output file and our tag at the end
     local out_squiluFreeze_handle = file(out_squiluFreeze, "wb+");
     out_squiluFreeze_handle.write(in_squiluFreeze_data);
-    out_squiluFreeze_handle.write(in_luafltkscript_data);
+    out_squiluFreeze_handle.write(in_squiluScript_data);
     // 2^32 = 4294967296, should be enough? Our data is 29 bytes from end.
-    out_squiluFreeze_handle.write(format("<DAD:%010d>ooOo(^.^)oOoo", in_luafltkscript_length));
+    out_squiluFreeze_handle.write(format("<DAD:%010d>ooOo(^.^)oOoo", in_squiluScript_length));
     out_squiluFreeze_handle.close();
 }
 

+ 75 - 4
SquiLu/sq/sq.c

@@ -331,7 +331,8 @@ void Interactive(HSQUIRRELVM v)
 // end of file is 29: "<DAD:%010d>ooOo(^.^)oOoo", script_len
 #define END_TAG_LEN 29
 
-static SQInteger LoadFrozenScript(HSQUIRRELVM v, const SQChar* filename, int only_check)
+#if 0
+static SQInteger LoadFrozenScript0(HSQUIRRELVM v, const SQChar* filename, int only_check)
 {
     SQInteger retval;
     // lots of debugging to make sure that everything is ok
@@ -398,7 +399,7 @@ static SQInteger LoadFrozenScript(HSQUIRRELVM v, const SQChar* filename, int onl
     //fwrite(script, 1, finalSize, f);
     //fclose(f);
 
-    if(SQ_SUCCEEDED(sq_compilebuffer(v,script, script_len, SQFalse, SQTrue))) {
+    if(SQ_SUCCEEDED(sq_compilebuffer(v,script, script_len, _SC("frozenScript"), SQTrue))) {
         int i, callargs = 1;
         sq_pushroottable(v);
         for(i=0;i<sq_main_argc;i++)
@@ -411,11 +412,11 @@ static SQInteger LoadFrozenScript(HSQUIRRELVM v, const SQChar* filename, int onl
             sq_getscratchpad(v,-1)[alen] = _SC('\0');
 #else
             a=sq_main_argv[i];
-#endif
+#endif
             sq_pushstring(v,a,-1);
             callargs++;
             //sq_arrayappend(v,-2);
-        }
+        }
         if(SQ_SUCCEEDED(sq_call(v,callargs,SQTrue,SQTrue))) {
             SQObjectType type = sq_gettype(v,-1);
             if(type == OT_INTEGER) {
@@ -432,6 +433,76 @@ static SQInteger LoadFrozenScript(HSQUIRRELVM v, const SQChar* filename, int onl
 
     return 0;
 }
+#endif
+
+static SQInteger LoadFrozenScript(HSQUIRRELVM v, const SQChar* filename, int only_check)
+{
+    SQInteger retval;
+    // lots of debugging to make sure that everything is ok
+    //printf("%s\n", filename);
+    FILE *f = fopen(filename, "rb");
+
+    if (!f) return -1;
+
+    if (fseek(f, 0, SEEK_END))
+    {
+        fclose(f);
+        return -1;
+    }
+
+	int fileSize = ftell(f);
+	//printf("%d\n", fileSize);
+    if (fseek(f, fileSize-END_TAG_LEN, SEEK_SET ))
+    {
+        fclose(f);
+        return -1;
+    }
+
+    // do some sanity checking before reading the script length
+
+    char tag_buf[END_TAG_LEN+2] = {0};
+    memset(tag_buf, 0, sizeof(char)*(END_TAG_LEN+2));
+    long script_len = 0;
+
+    if (fread((void*)tag_buf, 1, END_TAG_LEN, f) != END_TAG_LEN)
+    {
+        fclose(f);
+        return -1;
+    }
+
+	//printf("%s\n", tag_buf);
+    if (sscanf(tag_buf, SCRIPT_END_TAG, &script_len) != 1)
+    {
+        fclose(f);
+
+        // they only wanted to know if the script exists, assume it's valid
+        if (only_check) return -2;
+        else return -1;
+    }
+    else if (fseek(f, fileSize-END_TAG_LEN - script_len, SEEK_SET ))
+    {
+        fclose(f);
+        return -1;
+    }
+
+    fclose(f);
+    SQChar srcBoot[192];
+    SQInteger scr_len = scsnprintf(srcBoot, sizeof(srcBoot),
+            _SC("local __fd=file(\"%s\", \"rb\");__fd.seek(%d, 'b');local __zsrc=__fd.read(%d);__fd.close();dostring(zlib.inflate(__zsrc));"),
+            filename, fileSize-END_TAG_LEN - script_len, script_len);
+
+    if(SQ_SUCCEEDED(sq_compilebuffer(v,srcBoot, scr_len, _SC("bootScript"), SQTrue))) {
+        sq_pushroottable(v);
+        if(SQ_SUCCEEDED(sq_call(v, 1,SQFalse, SQTrue))) {
+            return _DONE;
+        }
+        else{
+            return _ERROR;
+        }
+    }
+
+    return 0;
+}
 
 
 SQRESULT sqext_register_sqfs(HSQUIRRELVM v);