Main.lua 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. local util = require 'xlua.util'
  2. App = CS.FairyEditor.App
  3. ProjectType = CS.FairyEditor.ProjectType
  4. fprint = function(msg)
  5. App.consoleView:Log(tostring(msg))
  6. end
  7. function printLeakingReferences()
  8. local str = 'These references from Lua to C# have not been released: \n'
  9. local registry = debug.getregistry()
  10. for k, v in pairs(registry) do
  11. if type(k) == 'number' and type(v) == 'function' and registry[v] == k then
  12. local info = debug.getinfo(v)
  13. str = str..string.format('%s:%d', info.short_src, info.linedefined)..'\n'
  14. end
  15. end
  16. str = str..'Try to solve it in a function with name \'onDestroy\'.'
  17. App.consoleView:LogWarning(str)
  18. end
  19. function fclass()
  20. local cls = {}
  21. cls.__index=cls
  22. function cls.new(...)
  23. local inst=setmetatable({}, cls)
  24. if inst.ctor then
  25. inst.ctor(inst, ...)
  26. end
  27. return inst
  28. end
  29. return cls
  30. end
  31. local codeGenerators = {
  32. [ProjectType.Flash] = require('GenCode_AS3'),
  33. [ProjectType.Starling] = require('GenCode_AS3'),
  34. [ProjectType.Layabox] = require('GenCode_TS'),
  35. [ProjectType.Egret] = require('GenCode_Egret'),
  36. [ProjectType.PIXI] = require('GenCode_PIXI'),
  37. [ProjectType.Unity] = require('GenCode_CSharp'),
  38. [ProjectType.CryEngine] = require('GenCode_CSharp'),
  39. [ProjectType.MonoGame] = require('GenCode_CSharp'),
  40. [ProjectType.Haxe] = require('GenCode_Haxe'),
  41. [ProjectType.Cocos2dx] = require('GenCode_CPP'),
  42. [ProjectType.Vision] = require('GenCode_CPP'),
  43. [ProjectType.CocosCreator] = require('GenCode_TS'),
  44. [ProjectType.ThreeJS] = require('GenCode_TS'),
  45. [ProjectType.CreateJS] = require('GenCode_TS')
  46. }
  47. function genCodeDefault(handler)
  48. local func = codeGenerators[handler.project.type]
  49. if func~=nil then
  50. func(handler)
  51. end
  52. end
  53. CodeWriter = fclass()
  54. function CodeWriter:ctor(config)
  55. config = config or {}
  56. self.blockStart = config.blockStart or '{'
  57. self.blockEnd = config.blockEnd or '}'
  58. self.blockFromNewLine = config.blockFromNewLine
  59. if self.blockFromNewLine==nil then self.blockFromNewLine = true end
  60. if config.usingTabs then
  61. self.indentStr = '\t'
  62. else
  63. self.indentStr = ' '
  64. end
  65. self.usingTabs = config.usingTabs
  66. self.endOfLine = config.endOfLine or '\n'
  67. self.fileMark = config.fileMark or '/** This is an automatically generated class by FairyGUI. Please do not modify it. **/'
  68. self.lines = {}
  69. self.indent = 0
  70. self:writeMark()
  71. end
  72. function CodeWriter:writeMark()
  73. table.insert(self.lines, self.fileMark)
  74. table.insert(self.lines, '')
  75. end
  76. function CodeWriter:writeln(format, ...)
  77. if not format then
  78. table.insert(self.lines, '')
  79. return
  80. end
  81. local str = ''
  82. for i=0,self.indent-1 do
  83. str = str..self.indentStr
  84. end
  85. str = str..string.format(format, ...)
  86. table.insert(self.lines, str)
  87. return self
  88. end
  89. function CodeWriter:startBlock()
  90. if self.blockFromNewLine or #self.lines==0 then
  91. self:writeln(self.blockStart)
  92. else
  93. local str = self.lines[#self.lines]
  94. self.lines[#self.lines] = str..' '..self.blockStart
  95. end
  96. self.indent = self.indent + 1
  97. return self
  98. end
  99. function CodeWriter:endBlock()
  100. self.indent = self.indent - 1
  101. self:writeln(self.blockEnd)
  102. return self
  103. end
  104. function CodeWriter:incIndent()
  105. self.indent = self.indent + 1
  106. return self
  107. end
  108. function CodeWriter:decIndent()
  109. self.indent = self.indent - 1
  110. return self
  111. end
  112. function CodeWriter:reset()
  113. if #self.lines>0 then self.lines = {} end
  114. self.indent = 0
  115. self:writeMark()
  116. end
  117. function CodeWriter:tostring()
  118. return table.concat(self.lines, self.endOfLine)
  119. end
  120. function CodeWriter:save(filePath)
  121. local str = table.concat(self.lines, self.endOfLine)
  122. CS.System.IO.File.WriteAllText(filePath, str)
  123. -- local file = io.open(filePath, 'w+b')
  124. -- io.output(file)
  125. -- io.write(str)
  126. -- io.close(file)
  127. end