Ver Fonte

prevent access to hide.Ide from plugins compiled with -D editor but not -D isIDE

ncannasse há 6 anos atrás
pai
commit
42a9857b2f

+ 1 - 0
hide.hxml

@@ -1,4 +1,5 @@
 common.hxml
+-D isIDE
 -js bin/hide.js
 -main hide.Ide
 --macro include("h3d.shader")

+ 10 - 1
hide/Ide.hx

@@ -1,8 +1,15 @@
 package hide;
 import hxd.inspect.Group;
 
+#if !isIDE
+// forbid access to Ide.inst when not compiled with -D isIDE
+// plugins should not access directly to hide.Ide!
+abstract ShouldNotAccess(Int) {}
+class Ide { public static var inst(get,never) : ShouldNotAccess; }
+#else
+
 @:expose
-class Ide {
+class Ide implements hide.prefab.EditContext.IdeApi {
 
 	public var currentConfig(get,never) : Config;
 	public var projectDir(get,never) : String;
@@ -978,3 +985,5 @@ class CustomLoader extends hxd.res.Loader {
 	}
 
 }
+
+#end

+ 11 - 2
hide/prefab/EditContext.hx

@@ -1,5 +1,14 @@
 package hide.prefab;
 
+interface IdeApi {
+	function getPath( relPath : String ) : String;
+	function makeRelative( path : String ) : String;
+	function error( message : String ) : Void;
+	function chooseFileSave( fileName : String, callb : Null<String> -> Void ) : Void;
+	function parseJSON( jsonStr : String ) : Dynamic;
+	function toJSON( obj : Dynamic ) : String;
+}
+
 class EditContext {
 
 	public var rootContext : Context;
@@ -9,11 +18,11 @@ class EditContext {
 	var updates : Array<Float->Void> = [];
 
 	public var prefabPath : String;
-	public var ide(get,never) : hide.Ide;
+	public var ide(get,never) : IdeApi;
 	public var scene : hide.comp.Scene;
 	public var properties : hide.comp.PropsEditor;
 	public var cleanups : Array<Void->Void>;
-	function get_ide() return hide.Ide.inst;
+	function get_ide() return #if isIDE hide.Ide.inst #else throw "assert" #end;
 	public function onChange(p : Prefab, propName : String) {
 		var ctx = getContext(p);
 		if(ctx != null) {

+ 1 - 1
hide/prefab/Noise.hx

@@ -218,7 +218,7 @@ class Noise extends Prefab {
 			ctx.ide.chooseFileSave("noise.png", function(f) if( f != null ) {
 				try {
 					var data = cast(ctx.getContext(this).local2d, h2d.Bitmap).tile.getTexture().capturePixels().toPNG();
-					sys.io.File.saveBytes(hide.Ide.inst.getPath(f), data);
+					sys.io.File.saveBytes(ctx.ide.getPath(f), data);
 				} catch( e : Dynamic ) {
 					ctx.ide.error(e);
 				}

+ 6 - 5
hide/prefab/Settings.hx

@@ -52,13 +52,14 @@ class Settings extends Prefab {
 			</dl>
 			<br/>
 		'),this);
+		var typesCache = hide.Ide.inst.typesCache; // direct reference (not in API)
 		var select = props.find("select");
-		var models = ctx.ide.typesCache.getModels();
+		var models = typesCache.getModels();
 		for( m in models )
-			new hide.Element('<option>').attr("value", m.id).text(ctx.ide.typesCache.getModelName(m)).appendTo(select);
+			new hide.Element('<option>').attr("value", m.id).text(typesCache.getModelName(m)).appendTo(select);
 		var modelDef = null;
 		if( modelType != null ) {
-			modelDef = ctx.ide.typesCache.get(modelType, true);
+			modelDef = typesCache.get(modelType, true);
 			if( modelDef == null )
 				new hide.Element('<option>').attr("value", modelType).text("?"+modelType).appendTo(select);
 			select.val(modelType);
@@ -104,8 +105,8 @@ class Settings extends Prefab {
 
 		ctx.properties.addProps(modelDef.fields, this.data);
 		var rebuild = ctx.rebuildProperties;
-		ctx.ide.typesCache.watch(rebuild);
-		ctx.cleanups.push(ctx.ide.typesCache.unwatch.bind(rebuild));
+		typesCache.watch(rebuild);
+		ctx.cleanups.push(typesCache.unwatch.bind(rebuild));
 	}
 
 	#end

+ 1 - 1
hide/prefab/Shader.hx

@@ -75,7 +75,7 @@ class Shader extends Prefab {
 	}
 
 	function fixSourcePath() {
-		#if editor
+		#if isIDE
 		var ide = hide.Ide.inst;
 		var shadersPath = ide.projectDir + "/src";  // TODO: serach in haxe.classPath?
 

+ 5 - 5
hide/prefab/terrain/TerrainEditor.hx

@@ -791,7 +791,7 @@ class TerrainEditor {
 			var img : Element;
 			if( i == currentBrush.index) img = new Element('<div class="surface-preview-selected"></div>');
 			else img = new Element('<div class="surface-preview"></div>');
-			var imgPath = hide.Ide.inst.getPath(surface.albedo.name);
+			var imgPath = ctx.ide.getPath(surface.albedo.name);
 			img.css("background-image", 'url("file://$imgPath")');
 			var surfaceElem = new Element('<div class=" surface"><span class="tooltiptext">$label</span></div>').prepend(img);
 			surfaceElem.contextmenu(function(e) {
@@ -838,7 +838,7 @@ class TerrainEditor {
 		for( i in 0 ... brushIcons.length){
 			var elem = new Element('<div class="terrain-brushMode"></div>');
 			var img = new Element('<div class="terrain-brushModeIcon"></div>');
-			img.css("background-image", 'url("file://${hide.Ide.inst.getPath("${HIDE}/res/" + brushIcons[i])}")');
+			img.css("background-image", 'url("file://${ctx.ide.getPath("${HIDE}/res/" + brushIcons[i])}")');
 			elem.prepend(img);
 			elem.click(function(_) {
 				var l = props.find(".terrain-brushModeIcon");
@@ -901,7 +901,7 @@ class TerrainEditor {
 		props.find(".save").click(function(_) {
 			var datPath = new haxe.io.Path(ctx.rootContext.shared.currentPath);
 			datPath.ext = "dat";
-			var fullPath = hide.Ide.inst.getPath(datPath.toString() + "/" + terrainPrefab.name);
+			var fullPath = ctx.ide.getPath(datPath.toString() + "/" + terrainPrefab.name);
 			if( sys.FileSystem.isDirectory(fullPath)){
 				var files = sys.FileSystem.readDirectory(fullPath);
 				for(file in files)
@@ -920,13 +920,13 @@ class TerrainEditor {
 				var img : Element;
 				if( brush.name == currentBrush.name) img = new Element('<div class="brush-preview-selected"></div>');
 				else img = new Element('<div class="brush-preview"></div>');
-				img.css("background-image", 'url("file://${hide.Ide.inst.getPath(brush.texture)}")');
+				img.css("background-image", 'url("file://${ctx.ide.getPath(brush.texture)}")');
 				var brushElem = new Element('<div class="brush"><span class="tooltiptext">$label</span></div>').prepend(img);
 				brushElem.click(function(e){
 					currentBrush.size = brush.size;
 					currentBrush.strength = brush.strength;
 					currentBrush.step = brush.step;
-					currentBrush.texPath = hide.Ide.inst.getPath(brush.texture);
+					currentBrush.texPath = ctx.ide.getPath(brush.texture);
 					currentBrush.tex = loadTexture(ctx, currentBrush.texPath);
 					currentBrush.name = brush.name;
 					if(currentBrush.bitmap != null){