Skip to content

Commit

Permalink
Added GMS2.3 support.
Browse files Browse the repository at this point in the history
  • Loading branch information
YellowAfterlife committed Nov 11, 2020
1 parent 1351fc2 commit 43fda27
Show file tree
Hide file tree
Showing 4 changed files with 439 additions and 26 deletions.
119 changes: 107 additions & 12 deletions src/GenExt2.hx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package;
import haxe.Json;
import haxe.io.Path;
import sys.FileSystem;
import sys.io.File;
import file.*;
import YyExtension;
Expand All @@ -12,15 +13,17 @@ import YyExtension;
class GenExt2 extends GenExt {
public var json:String;
public var yyExt:YyExtension;
public var v23:Bool;
override public function proc(filter:Array<String>) {
var dir = Path.directory(path);
if (FileSystem.exists(path + ".base")) {
json = File.getContent(path + ".base");
} else json = File.getContent(path);
v23 = json.indexOf('"resourceType": "GMExtension"') >= 0;
// GMS2 uses non-spec int64s in extensions JSON
json = ~/("copyToTargets":\s*)(\d{12,32})/g.replace(json, '$1"$2"');
//
yyExt = Json.parse(json);
yyExt = YyJsonParser.parse(json);
for (file in yyExt.files) {
var q:GenFile;
var filePath = Path.join([dir, file.filename]);
Expand All @@ -39,7 +42,9 @@ class GenExt2 extends GenExt {
q.functions.push(gf);
}
for (ym in file.constants) {
var gm = new GenMacro(ym.constantName, ym.value, ym.hidden, 0);
var ymName = ym.name;
if (ymName == null) ymName = ym.constantName;
var gm = new GenMacro(ymName, ym.value, ym.hidden, 0);
q.macros.push(gm);
}
}
Expand All @@ -48,15 +53,7 @@ class GenExt2 extends GenExt {
files.push(q);
}
}
override public function flush():Void {
var out = new YyBuf();
var filesStartStr = '"files": [';
var filesStart = json.indexOf(filesStartStr);
if (filesStart < 0) throw "Your extension doesn't have an array of files in it.";
if (json.indexOf("\r\n") < 0) out.newLine = "\n";
out.addString(json.substring(0, filesStart + filesStartStr.length));
out.depth = 2;
out.addLine(0);
function flush22(out:YyBuf):Void {
for (q in files) {
out.addSep();
out.objectOpen();
Expand Down Expand Up @@ -124,8 +121,106 @@ class GenExt2 extends GenExt {
out.addPair("uncompress", d.uncompress);
out.objectClose();
}
}
function flush23(out:YyBuf):Void {
var extName:String = yyExt.name;
var extPath = 'extensions/$extName/$extName.yy';
var fileSep = false;
for (q in files) {
if (fileSep) out.addLine(); else fileSep = true;
out.objectOpen();
var d:YyExtensionFile = q.data;
out.addPair("filename", d.filename);
out.addPair("origname", d.origname);
out.addPair("init", d.init);
out.addPair("final", Reflect.field(d, "final"));
out.addPair("kind", d.kind);
out.addPair("uncompress", d.uncompress);
//
out.addField("functions");
out.arrayOpen();
var fkin = q.funcKind;
var funcSep = false;
for (qf in q.functions) {
if (funcSep) out.addLine(); else funcSep = true;
out.objectOpen();
out.addPair("externalName", qf.extName);
out.addPair("kind", qf.comp == null ? 11 : fkin);
out.addPair("help", qf.comp != null ? qf.comp : "");
out.addPair("hidden", qf.comp == null);
out.addPair("returnType", qf.retType);
out.addPair("argCount", qf.argCount);
out.addPair("args", qf.argTypes);
out.addPair("resourceVersion", "1.0");
out.addPair("name", qf.name);
out.addPair("tags", []);
out.addPair("resourceType", "GMExtensionFunction");
out.objectClose();
out.add(",");
}
out.arrayClose();
out.addFieldEnd();
//
out.addField("constants");
out.arrayOpen();
var macroSep = false;
for (qm in q.macros) {
if (macroSep) out.addLine(); else macroSep = true;
out.objectOpen();
out.addPair("value", qm.value);
out.addPair("hidden", qm.hide);
out.addPair("resourceVersion", "1.0");
out.addPair("name", qm.name);
out.addPair("tags", []);
out.addPair("resourceType", "GMExtensionConstant");
out.objectClose();
out.add(",");
}
out.arrayClose();
out.addFieldEnd();
//
out.addPair("ProxyFiles", d.ProxyFiles);
out.addPair("copyToTargets", d.copyToTargets);
//
out.addField("order");
out.arrayOpen();
var orderSep = false;
for (qf in q.functions) {
if (orderSep) out.addLine(); else orderSep = true;
out.objectOpen();
out.addPair("name", qf.name);
out.addPair("path", extPath);
out.objectClose();
out.add(",");
}
out.arrayClose();
out.addFieldEnd();
//
out.addPair("resourceVersion", "1.0");
out.addPair("name", "");
out.addPair("tags", []);
out.addPair("resourceType", "GMExtensionFile");
//
out.objectClose();
out.addFieldEnd();
}
}
override public function flush():Void {
var out = new YyBuf(v23);
var filesStartStr = '"files": [';
var filesStart = json.indexOf(filesStartStr);
if (filesStart < 0) throw "Your extension doesn't have an array of files in it.";
//
if (json.indexOf("\r\n") < 0) out.newLine = "\n";
out.addString(json.substring(0, filesStart + filesStartStr.length));
out.depth = 2;
out.addLine(0);
//
if (v23) {
flush23(out);
} else flush22(out);
//
var filesEnd = json.indexOf('\n ],', filesStart);
var filesEnd = json.indexOf(v23 ? '\n ],' : '\n ],', filesStart);
if (filesEnd < 0) throw "Your extension doesn't have a well-balanced end of files array in it. It might be malformed.";
out.addString(json.substring(filesEnd));
json = out.toString();
Expand Down
61 changes: 48 additions & 13 deletions src/YyBuf.hx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
package;
import haxe.DynamicAccess;
import haxe.Json;

/**
Expand All @@ -9,12 +10,21 @@ class YyBuf extends StringBuf {
public var newLine:String = "\r\n";
public var depth:Int = 0;
public var sep:Array<Bool> = [false];
public var v23:Bool;
public function new(v23:Bool) {
super();
this.v23 = v23;
}
public function addLine(d:Int = 0) {
add(newLine);
if (d > 0) sep.push(false);
if (d < 0) sep.pop();
depth += d;
for (i in 0 ... depth) add(" ");
if (v23) {
for (i in 0 ... depth) add(" ");
} else {
for (i in 0 ... depth) add(" ");
}
}
//
public inline function addString(s:String) {
Expand All @@ -28,31 +38,52 @@ class YyBuf extends StringBuf {
} else sep[i] = true;
}
public function addField(fd:String) {
addSep();
if (!v23) addSep();
addChar('"'.code);
addString(fd);
addString('": ');
addString(v23 ? '":' : '": ');
}
public function addFieldEnd() {
if (v23) addString(",");
}
public function addValue(val:Dynamic) {
if (Std.is(val, Array)) {
var arr:Array<Dynamic> = val;
arrayOpen();
for (v in arr) {
addSep();
addValue(v);
if (v23 && arr.length == 0) {
addString("[]");
} else {
arrayOpen();
for (v in arr) {
addSep();
addValue(v);
}
if (v23) add(",");
arrayClose();
}
arrayClose();
} else if (Std.is(val, String)) {
}
else if (Std.is(val, String)) {
var s = Json.stringify(val);
s = StringTools.replace(s, "/", "\\/"); // off-spec
if (!v23) s = StringTools.replace(s, "/", "\\/"); // off-spec
addString(s);
} else {
}
else if (Reflect.isObject(val)) {
var fields = Reflect.fields(val);
if (fields.length != 0) {
objectOpen();
for (f in fields) {
addPair(f, Reflect.field(val, f));
}
objectClose();
} else addString("{}");
}
else {
addString(Json.stringify(val));
}
}
public function addPair(fd:String, val:Dynamic) {
addField(fd);
addValue(val);
addFieldEnd();
}
//
public function arrayOpen() {
Expand All @@ -66,10 +97,14 @@ class YyBuf extends StringBuf {
//
public function objectOpen() {
add("{");
addLine(1);
if (v23) {
depth++;
} else addLine(1);
}
public function objectClose() {
addLine(-1);
if (v23) {
depth--;
} else addLine(-1);
add("}");
}
}
3 changes: 2 additions & 1 deletion src/YyExtension.hx
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ typedef YyExtensionFunc = {
}
typedef YyExtensionMacro = {
>YyBase,
constantName:String,
?constantName:String,
?name:String,
hidden:Bool,
value:String,
}
Expand Down
Loading

0 comments on commit 43fda27

Please sign in to comment.