Config.hx 3.7 KB

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