Config.hx 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. package hide;
  2. typedef LayoutState = {
  3. var content : Any;
  4. var fullScreen : { name : String, state : Any };
  5. }
  6. typedef HideGlobalConfig = {
  7. var autoSaveLayout : Null<Bool>;
  8. var currentProject : String;
  9. var recentProjects : Array<String>;
  10. var windowPos : { x : Int, y : Int, w : Int, h : Int, max : Bool };
  11. @:optional var sceneEditorLayout : { colsVisible : Bool, colsCombined : Bool };
  12. }
  13. typedef HideProjectConfig = {
  14. var layouts : Array<{ name : String, state : LayoutState }>;
  15. var renderer : String;
  16. var dbCategories : Array<String>;
  17. };
  18. typedef ConfigDef = {
  19. var hide : {};
  20. };
  21. class Config {
  22. var ide : Ide;
  23. var parent : Config;
  24. public var path(default,null) : String;
  25. public var source(default, null) : ConfigDef;
  26. public var current : ConfigDef;
  27. public function new( ?parent : Config ) {
  28. ide = Ide.inst;
  29. this.parent = parent;
  30. sync();
  31. }
  32. public function isLocal() {
  33. if( path == null && parent != null ) return parent.isLocal();
  34. return path == null || StringTools.startsWith(path, ide.projectDir);
  35. }
  36. public function load( path : String ) {
  37. this.path = path;
  38. var fullPath = ide.getPath(path);
  39. if( sys.FileSystem.exists(fullPath) )
  40. source = try ide.parseJSON(sys.io.File.getContent(fullPath)) catch( e : Dynamic ) throw e+" (in "+fullPath+")";
  41. else
  42. source = cast {};
  43. sync();
  44. }
  45. public function save() {
  46. sync();
  47. if( path == null ) throw "Cannot save properties (unknown path)";
  48. var fullPath = ide.getPath(path);
  49. if( Reflect.fields(source).length == 0 )
  50. try sys.FileSystem.deleteFile(fullPath) catch( e : Dynamic ) {};
  51. else
  52. sys.io.File.saveContent(fullPath, ide.toJSON(source));
  53. }
  54. public function sync() {
  55. if( parent != null ) parent.sync();
  56. current = cast {};
  57. if( parent != null ) merge(parent.current);
  58. if( source != null ) merge(source);
  59. }
  60. function merge( value : Dynamic ) {
  61. mergeRec(current, value);
  62. }
  63. function mergeRec( dst : Dynamic, src : Dynamic ) {
  64. for( f in Reflect.fields(src) ) {
  65. var v : Dynamic = Reflect.field(src,f);
  66. var t : Dynamic = Reflect.field(dst, f);
  67. if( Type.typeof(v) == TObject ) {
  68. if( t == null ) {
  69. t = {};
  70. Reflect.setField(dst, f, t);
  71. }
  72. mergeRec(t, v);
  73. } else if( v == null )
  74. Reflect.deleteField(dst, f);
  75. else
  76. Reflect.setField(dst,f,v);
  77. }
  78. }
  79. public function get( key : String, ?defaultVal : Dynamic ) : Dynamic {
  80. var val = Reflect.field(current,key);
  81. if(val != null) return val;
  82. return defaultVal;
  83. }
  84. public function getLocal( key : String, ?defaultVal : Dynamic ) : Dynamic {
  85. var v = get(key);
  86. if( v == null ) return defaultVal;
  87. if( isLocal() ) return v;
  88. if( parent == null ) return defaultVal;
  89. return parent.getLocal(key,defaultVal);
  90. }
  91. public function set( key : String, val : Dynamic ) {
  92. if( val == null )
  93. Reflect.deleteField(source, key);
  94. else
  95. Reflect.setField(source, key, val);
  96. save();
  97. }
  98. public static function loadForProject( projectPath : String, resourcePath : String ) {
  99. var hidePath = Ide.inst.appPath;
  100. var defaults = new Config();
  101. defaults.load(hidePath + "/defaultProps.json");
  102. var userGlobals = new Config(defaults);
  103. userGlobals.load(hidePath + "/props.json");
  104. if( userGlobals.source.hide == null )
  105. userGlobals.source.hide = {
  106. autoSaveLayout : true,
  107. layouts : null,
  108. recentProjects : [],
  109. currentProject : "",
  110. windowPos : null,
  111. renderer : null,
  112. };
  113. var perProject = new Config(userGlobals);
  114. perProject.load(resourcePath + "/props.json");
  115. var projectUserCustom = new Config(perProject);
  116. projectUserCustom.load(nw.App.dataPath + "/" + projectPath.split("\\").join("/").split("/").join("_").split(":").join("_") + ".json");
  117. var p = projectUserCustom;
  118. if( p.source.hide == null )
  119. p.source.hide = ({ layouts : [], renderer : null, dbCategories: null } : HideProjectConfig);
  120. var current = new Config(projectUserCustom);
  121. return {
  122. global : userGlobals,
  123. project : perProject,
  124. user : projectUserCustom,
  125. current : current,
  126. };
  127. }
  128. public static function loadForFile( ide : hide.Ide, path : String ) {
  129. var parts = path.split("/");
  130. var propFiles = [];
  131. var first = true, allowSave = false;
  132. while( true ) {
  133. var pfile = ide.getPath(parts.join("/") + "/props.json");
  134. if( sys.FileSystem.exists(pfile) ) {
  135. propFiles.unshift(pfile);
  136. if( first ) allowSave = true;
  137. }
  138. if( parts.length == 0 ) break;
  139. first = false;
  140. parts.pop();
  141. }
  142. var parent = ide.currentConfig;
  143. for( p in propFiles ) {
  144. parent = new Config(parent);
  145. parent.load(p);
  146. }
  147. return allowSave ? parent : new Config(parent);
  148. }
  149. }