├── README.md └── garbage_code_generater.lua /README.md: -------------------------------------------------------------------------------- 1 | C# Garbage Code Generater 2 | ------------------------- 3 | -h: Help 4 | -o: Output Dir, Default="garbate" 5 | -c: Generate Class Count, Default=400 6 | -mc: Method Count, Default=30 7 | -pc: Property Count, Default=30 8 | -ac: Attribute Count, Default=30 9 | -verbose: Print Every Generate Class Name 10 | -q: QuietMode, Print Nothing 11 | 12 | ex. lua garbage_code_generater.lua -o output -c 100 13 | ex. lua garbage_code_generater.lua 14 | 15 | C#垃圾代码生成器 16 | -------------- 17 | -h: 帮助 18 | -o: 输出路径, 默认值="garbate" 19 | -c: 生成类数量, 默认值=400 20 | -mc: 类方法数量, 默认值=30 21 | -pc: 类变量数量, 默认值=30 22 | -ac: 类属性数量, 默认值=30 23 | -verbose: 输出每一个类文件名 24 | -q: 安静模式, 不输出任何东西 25 | 26 | 例子. lua garbage_code_generater.lua -o output -c 100 27 | 例子. lua garbage_code_generater.lua 28 | 29 | 该工具用于生成大量垃圾代码。鄙人的一个Unity项目因为要向Apple提交马甲包,最终代码有不能过于相似,所以在正常代码里混入垃圾代码。 30 | 用法为:用工具生成垃圾代码后,把输出路径直接复制到Unity项目路径中,然后导出Xcode工程 31 | -------------------------------------------------------------------------------- /garbage_code_generater.lua: -------------------------------------------------------------------------------- 1 | --[[ 2 | Copyright 2017 xerysherry 3 | 4 | Permission is hereby granted, free of charge, to any person 5 | obtaining a copy of this software and associated documentation 6 | files (the "Software"), to deal in the Software without restriction, 7 | including without limitation the rights to use, copy, modify, 8 | merge, publish, distribute, sublicense, and/or sell copies of 9 | the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall 13 | be included in all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 17 | OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 18 | IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE 19 | FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 20 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR 21 | IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 22 | IN THE SOFTWARE. 23 | 24 | garbage_code_generater.lua: Generate Generate CSharp Code 25 | Author: 26 | xerysherry 27 | ]] 28 | 29 | local os = os 30 | local math = math 31 | local io = io 32 | 33 | math.randomseed(os.time()) 34 | 35 | local charlist = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890_" 36 | local charlist_length = string.len(charlist) 37 | 38 | local header_charlist = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_" 39 | local header_charlist_length = string.len(header_charlist) 40 | 41 | local function getChar(chars, charscount, i) 42 | local idx = i % charscount + 1 43 | return string.sub(chars, idx, idx); 44 | end 45 | 46 | local function getRandomChar() 47 | return getChar(charlist, charlist_length, math.random(1, charlist_length)) 48 | end 49 | 50 | local function getRandomHChar() 51 | return getChar(header_charlist, header_charlist_length, math.random(1, charlist_length)) 52 | end 53 | 54 | local function getRandomName(l) 55 | local r = getRandomHChar() 56 | for _=2, l do 57 | r = r .. getRandomChar() 58 | end 59 | return r 60 | end 61 | 62 | local base_type_and_default = { 63 | ["bool"] = "false", 64 | ["int"] = "0", 65 | ["float"] = "0.0f", 66 | ["double"] = "0.0", 67 | ["string"] = "null" 68 | } 69 | 70 | local base_type_randomfunction = 71 | { 72 | ["bool"] = function(a, b, c) 73 | local r = math.random(1, 5) 74 | if r == 1 then 75 | return {a .. " = " .. b .. " && " .. c ..";", } 76 | elseif r == 2 then 77 | return { 78 | "if(" .. a .. ") ", 79 | "{", 80 | " ".. b.. " = !" .. c .. ";", 81 | "}", 82 | } 83 | elseif r == 3 then 84 | return { 85 | "if(" .. a .. " && " .. c .. ") ", 86 | "{", 87 | " ".. b.. " = !" .. b .. ";", 88 | "}", 89 | } 90 | elseif r == 4 then 91 | return { 92 | "if(" .. a .. " || " .. b .. ") ", 93 | "{", 94 | " ".. b.. " = !" .. b .. ";", 95 | "}", 96 | } 97 | elseif r == 5 then 98 | return {a .. " = " .. b .. " || " .. c ..";", } 99 | else 100 | return {a .. " = " .. b .. " && " .. c ..";", } 101 | end 102 | end, 103 | ["int"] = function(a, b, c) 104 | local r = math.random(1, 7) 105 | if r==1 then 106 | return {a .. " = " .. b .. " + " .. c ..";", } 107 | elseif r==2 then 108 | return {a .. " = " .. b .. " - " .. c ..";", } 109 | elseif r==3 then 110 | return {a .. " = " .. b .. " * " .. c ..";", } 111 | elseif r==4 then 112 | return {a .. " = " .. b .. " / " .. c ..";", } 113 | elseif r==5 then 114 | return { 115 | a .. " = " .. math.random(1, 100000) ..";", 116 | b .. " = " .. math.random(1, 100000) ..";", 117 | c .. " = " .. math.random(1, 100000) ..";", 118 | } 119 | elseif r==6 then 120 | return { 121 | "for(int i=0;i<"..a..";++i)", 122 | "{", 123 | " "..b .."+=1;", 124 | " "..c .."+=" .. b..";", 125 | "}", 126 | } 127 | else 128 | return { 129 | b .. " = " .. a .. ";", 130 | c .. " = " .. a .. ";", 131 | } 132 | end 133 | end, 134 | ["float"] = function(a, b, c) 135 | local r = math.random(1, 6) 136 | if r==1 then 137 | return {a .. " = " .. b .. " + " .. c ..";", } 138 | elseif r==2 then 139 | return {a .. " = " .. b .. " - " .. c ..";", } 140 | elseif r==3 then 141 | return {a .. " = " .. b .. " * " .. c ..";", } 142 | elseif r==4 then 143 | return {a .. " = " .. b .. " / " .. c ..";", } 144 | elseif r==5 then 145 | return { 146 | a .. " = " .. math.random(1, 10000) ..".0f;", 147 | b .. " = " .. math.random(1, 10000) ..".0f;", 148 | c .. " = " .. math.random(1, 10000) ..".0f;", 149 | } 150 | else 151 | return { 152 | b .. " = " .. a .. ";", 153 | c .. " = " .. a .. ";", 154 | } 155 | end 156 | end, 157 | ["double"] = function(a, b, c) 158 | local r = math.random(1, 6) 159 | if r==1 then 160 | return {a .. " = " .. b .. " + " .. c ..";", } 161 | elseif r==2 then 162 | return {a .. " = " .. b .. " - " .. c ..";", } 163 | elseif r==3 then 164 | return {a .. " = " .. b .. " * " .. c ..";", } 165 | elseif r==4 then 166 | return {a .. " = " .. b .. " / " .. c ..";", } 167 | elseif r==5 then 168 | return { 169 | a .. " = " .. math.random(1, 10000) ..".0;", 170 | b .. " = " .. math.random(1, 10000) ..".0;", 171 | c .. " = " .. math.random(1, 10000) ..".0;", 172 | } 173 | else 174 | return { 175 | b .. " = " .. a .. ";", 176 | c .. " = " .. a .. ";", 177 | } 178 | end 179 | end, 180 | ["string"] = function(a, b, c) 181 | local r = math.random(1, 3) 182 | if r==1 then 183 | return {a .. " = " .. b .. " + " .. c ..";", } 184 | elseif r==2 then 185 | return { 186 | a .. " = string.Format("..c ..","..b..");", 187 | } 188 | else 189 | return { 190 | b .. " = " .. a .. ";", 191 | c .. " = " .. a .. ";", 192 | } 193 | end 194 | end, 195 | } 196 | 197 | local base_type_list = nil 198 | local function getRandomType() 199 | if base_type_list == nil then 200 | base_type_list = {} 201 | for k, _ in pairs(base_type_and_default) do 202 | table.insert(base_type_list, k) 203 | end 204 | end 205 | local r = math.random(1, #base_type_list) 206 | local t = base_type_list[r] 207 | return t, base_type_and_default[t] 208 | end 209 | local function getRandomPublic() 210 | local r = math.random(1,3) 211 | if r==1 then 212 | return "public" 213 | elseif r==2 then 214 | return "private" 215 | else 216 | return "" 217 | end 218 | end 219 | 220 | local property_info = 221 | { 222 | p = "", 223 | name = "default", 224 | t = "int", 225 | v = "0", 226 | } 227 | function PropertyGenerate(p, n, t, v) 228 | local ta = setmetatable({}, 229 | { 230 | __index = property_info, 231 | }) 232 | 233 | ta.p = p or ta.p 234 | ta.name = n 235 | ta.t = t 236 | ta.v = v 237 | return ta 238 | end 239 | 240 | local attribute_info = 241 | { 242 | p = "", 243 | name = "default", 244 | t = "int", 245 | property_info = nil, 246 | } 247 | function AttributeGenerate(p, n, t, pi) 248 | local ta = setmetatable({}, 249 | { 250 | __index = attribute_info, 251 | }) 252 | 253 | ta.p = p or ta.p 254 | ta.name = n 255 | ta.property_info = pi 256 | if pi~=nil then 257 | ta.t = pi.t 258 | end 259 | return ta 260 | end 261 | 262 | local method_info = 263 | { 264 | p = "", 265 | 266 | name = "default", 267 | --[[ 268 | {{"type1", "name1"}, {"type2", "name2"}, ..} 269 | ]] 270 | params = nil, 271 | --[[ 272 | {"type", "value"} 273 | ]] 274 | retn = nil, 275 | 276 | content = nil, 277 | } 278 | function MethodContentGenerate(ci) 279 | 280 | content = {} 281 | 282 | local function getRandomP(t) 283 | return t[math.random(1, #t)] 284 | end 285 | 286 | for i=1, 10 do 287 | local t = getRandomType() 288 | local m = ci.typemap[t] 289 | local f = base_type_randomfunction[t] 290 | if #m > 0 then 291 | local a = getRandomP(m) 292 | local c = f(a, getRandomP(m),getRandomP(m)) 293 | for _, v in ipairs(c) do 294 | table.insert(content, v) 295 | end 296 | end 297 | end 298 | 299 | return content 300 | end 301 | 302 | function MethodGenerate(ci, p, n, r) 303 | local ta = setmetatable({}, 304 | { 305 | __index = method_info, 306 | }) 307 | 308 | ta.p = p or ta.p 309 | ta.name = n 310 | ta.retn = r 311 | ta.content = MethodContentGenerate(ci) 312 | return ta 313 | end 314 | 315 | local class_info = 316 | { 317 | p = "", 318 | name = "default", 319 | implement = nil, 320 | 321 | properties = nil, 322 | attributes = nil, 323 | 324 | typemap = nil, 325 | } 326 | 327 | local classes = {} 328 | 329 | local classnames = {} 330 | function GetNextClassName(min, max) 331 | local r = math.random(min or 10, max or 10) 332 | while true do 333 | local n = getRandomName(r) 334 | if classnames[n] == nil then 335 | classnames[n] = true 336 | return n 337 | end 338 | end 339 | end 340 | 341 | --[[ 342 | - 类生成函数 343 | - 参数 344 | mc: 函数数量 345 | pc: 变量数量 346 | ac: 属性数量 347 | ]] 348 | function ClassGenerater(mc, pc, ac) 349 | local class = setmetatable({}, 350 | { 351 | __index = class_info, 352 | }) 353 | 354 | local usedname = {} 355 | function GetNextName(min, max) 356 | local r = math.random(min or 10, max or 10) 357 | while true do 358 | local n = getRandomName(r) 359 | if usedname[n] == nil then 360 | usedname[n] = true 361 | return n 362 | end 363 | end 364 | end 365 | 366 | class.name = GetNextClassName(10, 40) 367 | usedname[class.name] = true; 368 | --class.implement = "MonoBehaviour" 369 | 370 | class.properties = {} 371 | for i=1, pc or 20 do 372 | table.insert(class.properties, 373 | PropertyGenerate( 374 | getRandomPublic(), 375 | GetNextName(10, 40), 376 | getRandomType() 377 | )) 378 | end 379 | 380 | class.attributes = {} 381 | for i=1, ac or 20 do 382 | local t, v = getRandomType() 383 | local pi = nil 384 | if math.random(10) < 3 then 385 | pi = class.properties[math.random(#class.properties)] 386 | end 387 | table.insert(class.attributes, 388 | AttributeGenerate( 389 | "public", 390 | GetNextName(10, 40), 391 | t, pi 392 | )) 393 | end 394 | 395 | class.typemap = {} 396 | for _, v in ipairs(base_type_list) do 397 | class.typemap[v] = {} 398 | end 399 | for _, v in ipairs(class.properties) do 400 | table.insert(class.typemap[v.t], v.name) 401 | end 402 | for _, v in ipairs(class.attributes) do 403 | table.insert(class.typemap[v.t], v.name) 404 | end 405 | 406 | class.methods = {} 407 | for i=1, mc or 20 do 408 | local t, v = getRandomType() 409 | local pi = nil 410 | if math.random(10) < 3 then 411 | pi = class.properties[math.random(#class.properties)] 412 | end 413 | table.insert(class.methods, 414 | MethodGenerate(class, 415 | "public", 416 | GetNextName(10, 40) 417 | )) 418 | end 419 | 420 | return class 421 | end 422 | 423 | function GetClassSource(class) 424 | 425 | local r = "" 426 | local t = 0 427 | local s = function(str) 428 | for i=1, t do 429 | r = r.." " 430 | end 431 | r = r..str.."\r\n" 432 | end 433 | local _property = function(p) 434 | s(p.p .. " " .. p.t .. " " .. p.name .. " = " .. p.v .. ";") 435 | end 436 | local _attribute = function(a) 437 | s(a.p .. " " .. a.t .. " " .. a.name) 438 | s "{" 439 | t = t +1 440 | 441 | if a.property_info then 442 | local p = a.property_info 443 | s("get { return " .. p.name .. "; }") 444 | s("set { " .. p.name .. " = value; }") 445 | else 446 | s("get;") 447 | s("set;") 448 | end 449 | 450 | t = t -1 451 | s "}" 452 | end 453 | local _method = function(m) 454 | local ty = "void" 455 | if m.retn ~= nil then 456 | ty = m.retn[1] 457 | end 458 | local p = "()" 459 | if m.params ~= nil then 460 | p = "" 461 | for _, v in ipairs(m.params) do 462 | if string.len(p) > 0 then 463 | p = p .. "," .. v[1] .. " " .. v[2] 464 | else 465 | p = v[1] .. " " .. v[2] 466 | end 467 | end 468 | p = "(" .. p .. ")" 469 | end 470 | 471 | s(m.p.." ".. ty .." ".. m.name .. p) 472 | s"{" 473 | t = t+1 474 | 475 | for _, v in ipairs(m.content) do 476 | s(v) 477 | end 478 | t = t-1 479 | s"}" 480 | end 481 | 482 | s(class.p .. " class "..class.name..((class.implement~=nil and (" : "..class.implement)) or "")) 483 | s"{" 484 | t = t+1 485 | 486 | if class.properties ~= nil then 487 | for _, p in ipairs(class.properties) do 488 | _property(p) 489 | end 490 | end 491 | if class.attributes ~= nil then 492 | for _, p in ipairs(class.attributes) do 493 | _attribute(p) 494 | end 495 | end 496 | if class.methods ~= nil then 497 | for _, m in ipairs(class.methods) do 498 | _method(m) 499 | end 500 | end 501 | t = t-1 502 | s"}" 503 | 504 | return r 505 | end 506 | 507 | local function precent_bar(v, l) 508 | local p = {".", "-", "="} 509 | local pl = #p 510 | local line = "" 511 | local per = v 512 | local s = per*l 513 | local n = math.floor(s) 514 | local ns = math.floor((s - n) * pl)+1 515 | for i=1, l do 516 | if i