├── .gitignore ├── LICENSE ├── README.md └── dump-csharp.lua /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled Lua sources 2 | luac.out 3 | 4 | # luarocks build files 5 | *.src.rock 6 | *.zip 7 | *.tar.gz 8 | 9 | # Object files 10 | *.o 11 | *.os 12 | *.ko 13 | *.obj 14 | *.elf 15 | 16 | # Precompiled Headers 17 | *.gch 18 | *.pch 19 | 20 | # Libraries 21 | *.lib 22 | *.a 23 | *.la 24 | *.lo 25 | *.def 26 | *.exp 27 | 28 | # Shared objects (inc. Windows DLLs) 29 | *.dll 30 | *.so 31 | *.so.* 32 | *.dylib 33 | 34 | # Executables 35 | *.exe 36 | *.out 37 | *.app 38 | *.i*86 39 | *.x86_64 40 | *.hex 41 | 42 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # lua-dump-scripts -------------------------------------------------------------------------------- /dump-csharp.lua: -------------------------------------------------------------------------------- 1 | local DUMP_FOLDER = "X:/dump" 2 | local DUMP_LOG_FILE = DUMP_FOLDER .. "/dump-csharp.log" 3 | local DUMP_CS_FILE = DUMP_FOLDER .. "/dump-csharp.cs" 4 | 5 | local log = io.open(DUMP_LOG_FILE, "w") 6 | 7 | local function split(inputstr, sep) 8 | if sep == nil then 9 | sep = "%s" 10 | end 11 | local t={} 12 | for str in string.gmatch(inputstr, "([^"..sep.."]+)") do 13 | table.insert(t, str) 14 | end 15 | return t 16 | end 17 | 18 | local get_rvas = nil 19 | -- local get_rvas = function(index) 20 | -- return split(CS.MiHoYo.SDK.SDKUtil.RSAEncrypt("get_rva", string.format("%d", index)), ";") 21 | -- end 22 | 23 | local SYSTEM_NAMES = { 24 | ["System.Int32"] = "int", 25 | ["System.UInt32"] = "uint", 26 | ["System.Int16"] = "short", 27 | ["System.UInt16"] = "ushort", 28 | ["System.Int64"] = "long", 29 | ["System.UInt64"] = "ulong", 30 | ["System.Byte"] = "byte", 31 | ["System.SByte"] = "sbyte", 32 | ["System.Boolean"] = "bool", 33 | ["System.Single"] = "float", 34 | ["System.Double"] = "double", 35 | ["System.String"] = "string", 36 | ["System.Char"] = "char", 37 | ["System.Object"] = "object", 38 | ["System.Void"] = "void" 39 | } 40 | 41 | local TypeAttributes = CS.System.Reflection.TypeAttributes 42 | 43 | local function get_type_visibility_string(type) 44 | local visibility = type.Attributes & TypeAttributes.VisibilityMask 45 | if (visibility == TypeAttributes.Public) -- 46 | or (visibility == TypeAttributes.NestedPublic) then 47 | return "public " 48 | elseif (visibility == TypeAttributes.NotPublic) -- 49 | or (visibility == TypeAttributes.NestedFamANDAssem) -- 50 | or (visibility == TypeAttributes.NestedAssembly) then 51 | return "internal " 52 | elseif (visibility == TypeAttributes.NestedPrivate) then 53 | return "private " 54 | elseif (visibility == TypeAttributes.NestedFamily) then 55 | return "protected " 56 | elseif (visibility == TypeAttributes.NestedFamORAssem) then 57 | return "protected internal " 58 | else 59 | return "" 60 | end 61 | end 62 | 63 | local function get_type_string(type) 64 | local out = get_type_visibility_string(type) 65 | local attributes = type.Attributes 66 | if (attributes & TypeAttributes.Abstract).value__ ~= 0 -- 67 | and (attributes & TypeAttributes.Sealed).value__ ~= 0 then 68 | out = out .. "static " 69 | elseif (attributes & TypeAttributes.Abstract).value__ ~= 0 -- 70 | and (attributes & TypeAttributes.Interface).value__ == 0 then 71 | out = out .. "abstract " 72 | elseif (attributes & TypeAttributes.Sealed).value__ ~= 0 -- 73 | and (not type.IsEnum or not type.IsValueType) then 74 | out = out .. "sealed " 75 | end 76 | if (attributes & TypeAttributes.Interface).value__ ~= 0 then 77 | out = out .. "interface " 78 | elseif type.IsEnum then 79 | out = out .. "enum " 80 | elseif type.IsValueType then 81 | out = out .. "struct " 82 | else 83 | out = out .. "class " 84 | end 85 | return out 86 | end 87 | 88 | local function get_reflected_type(type) 89 | local name = type.Name 90 | if type.ReflectedType ~= nil -- 91 | and not type.ReflectedType.IsGenericType then 92 | name = get_reflected_type(type.ReflectedType) .. "." .. name 93 | end 94 | return name 95 | end 96 | 97 | local function get_runtime_type_name(type, alias) 98 | if type.IsArray then 99 | local out = get_runtime_type_name(type:GetElementType(), alias) 100 | out = out .. "[" 101 | for i = 2, type:GetArrayRank() do 102 | out = out .. "," 103 | end 104 | out = out .. "]" 105 | return out 106 | elseif type.IsPointer then 107 | return get_runtime_type_name(type:GetElementType(), alias) .. "*" 108 | elseif type.IsByRef then 109 | return get_runtime_type_name(type:GetElementType(), alias) .. "&" 110 | elseif type.IsGenericType then 111 | local name = type:GetGenericTypeDefinition().Name 112 | local pos = name:find("`") 113 | if pos ~= nil then 114 | name = name:sub(1, pos - 1) 115 | end 116 | local generic_args = type:GetGenericArguments() 117 | name = name .. "<" 118 | for i = 0, generic_args.Length - 1 do 119 | if i ~= 0 then 120 | name = name .. ", " 121 | end 122 | name = name .. get_runtime_type_name(generic_args[i], alias) 123 | end 124 | name = name .. ">" 125 | return name 126 | else 127 | if alias and type.Namespace == "System" then 128 | local name = SYSTEM_NAMES[type.FullName] 129 | if name ~= nil then 130 | return name 131 | end 132 | end 133 | return get_reflected_type(type) 134 | end 135 | end 136 | 137 | local function get_runtime_type_name_alias(type) 138 | return get_runtime_type_name(type, true) 139 | end 140 | 141 | local FieldAttributes = CS.System.Reflection.FieldAttributes 142 | 143 | local function get_field_type_string(field) 144 | local out = "" 145 | local access = field.Attributes & FieldAttributes.FieldAccessMask 146 | if (access == FieldAttributes.Private) then 147 | out = out .. "private " 148 | elseif (access == FieldAttributes.Public) then 149 | out = out .. "public " 150 | elseif (access == FieldAttributes.Family) then 151 | out = out .. "protected " 152 | elseif (access == FieldAttributes.Assembly) -- 153 | or (access == FieldAttributes.FamANDAssem) then 154 | out = out .. "internal " 155 | elseif (access == FieldAttributes.FamORAssem) then 156 | out = out .. "protected internal " 157 | end 158 | if field.IsLiteral then 159 | out = out .. "const " 160 | else 161 | if field.IsStatic then 162 | out = out .. "static " 163 | end 164 | if field.IsInitOnly then 165 | out = out .. "readonly " 166 | end 167 | end 168 | return out 169 | end 170 | 171 | local MethodAttributes = CS.System.Reflection.MethodAttributes 172 | 173 | local function get_method_type_string(method) 174 | local out = "" 175 | local attributes = method.Attributes 176 | local access = attributes & MethodAttributes.MemberAccessMask 177 | if (access == MethodAttributes.Private) then 178 | out = out .. "private " 179 | elseif (access == MethodAttributes.Public) then 180 | out = out .. "public " 181 | elseif (access == MethodAttributes.Family) then 182 | out = out .. "protected " 183 | elseif (access == MethodAttributes.Assembly) -- 184 | or (access == MethodAttributes.FamANDAssem) then 185 | out = out .. "internal " 186 | elseif (access == MethodAttributes.FamORAssem) then 187 | out = out .. "protected internal " 188 | end 189 | if (attributes & MethodAttributes.Static).value__ ~= 0 then 190 | out = out .. "static " 191 | end 192 | if (attributes & MethodAttributes.Abstract).value__ ~= 0 then 193 | out = out .. "abstract " 194 | if (attributes & MethodAttributes.VtableLayoutMask) == MethodAttributes.ReuseSlot then 195 | out = out .. "override " 196 | end 197 | elseif (attributes & MethodAttributes.Final).value__ ~= 0 then 198 | if (attributes & MethodAttributes.VtableLayoutMask) == MethodAttributes.ReuseSlot then 199 | out = out .. "sealed override " 200 | end 201 | elseif (attributes & MethodAttributes.Virtual).value__ ~= 0 then 202 | if (attributes & MethodAttributes.VtableLayoutMask) == MethodAttributes.NewSlot then 203 | out = out .. "virtual " 204 | else 205 | out = out .. "override " 206 | end 207 | end 208 | if (attributes & MethodAttributes.PinvokeImpl).value__ ~= 0 then 209 | out = out .. "extern " 210 | end 211 | return out 212 | end 213 | 214 | local function do_dump_csharp_field(file, field) 215 | file:write(get_field_type_string(field)) 216 | file:write(get_runtime_type_name_alias(field.FieldType) .. " ") 217 | file:write(field.Name) 218 | if field.IsLiteral then 219 | local value = field:GetRawConstantValue() 220 | if field.FieldType.FullName == "System.String" then 221 | -- TODO: fix utf-8 encoding 222 | file:write(string.format(" = \"%s\";", value)) 223 | elseif field.FieldType.FullName == "System.Char" then 224 | file:write(string.format(" = '\\x%X';", value)) 225 | else 226 | file:write(string.format(" = %s;", value)) 227 | end 228 | else 229 | local value = field:GetFieldOffset() 230 | if value & 0x8000000000000000 ~= 0 then 231 | value = -value 232 | end 233 | value = value + 0x10 234 | if value < 0 then 235 | file:write(string.format("; // -0x%X", value)) 236 | else 237 | file:write(string.format("; // 0x%X", value)) 238 | end 239 | end 240 | file:write("\n") 241 | end 242 | 243 | local function do_dump_csharp_property(file, property) 244 | if property.CanRead then 245 | local method = property:GetGetMethod(true) 246 | if method ~= nil then 247 | file:write(get_method_type_string(method)) 248 | else 249 | log:write("property " .. property.Name .. " has no getter\n") 250 | file:write(get_type_visibility_string(property.PropertyType)) 251 | end 252 | elseif property.CanWrite then 253 | local method = property:GetSetMethod(true) 254 | if method ~= nil then 255 | file:write(get_method_type_string(method)) 256 | else 257 | log:write("property " .. property.Name .. " has no setter\n") 258 | file:write(get_type_visibility_string(property.PropertyType)) 259 | end 260 | else 261 | file:write(get_type_visibility_string(property.PropertyType)) 262 | end 263 | file:write(get_runtime_type_name_alias(property.PropertyType) .. " ") 264 | file:write(property.Name .. " { ") 265 | if property.CanRead then 266 | file:write("get; ") 267 | end 268 | if property.CanWrite then 269 | file:write("set; ") 270 | end 271 | file:write("}\n") 272 | end 273 | 274 | local function do_dump_csharp_method(file, type, method, rva, is_ctor) 275 | if is_ctor then 276 | file:write(get_method_type_string(method)) 277 | file:write("void " .. method.Name) 278 | else 279 | file:write(get_method_type_string(method)) 280 | file:write(get_runtime_type_name_alias(method.ReturnType) .. " ") 281 | file:write(method.Name) 282 | local arguments = method:GetGenericArguments() 283 | if arguments.Length > 0 then 284 | file:write("<") 285 | for i = 0, arguments.Length - 1 do 286 | local argument = arguments[i] 287 | if i ~= 0 then 288 | file:write(", ") 289 | end 290 | file:write(get_runtime_type_name_alias(argument)) 291 | end 292 | file:write(">") 293 | end 294 | end 295 | file:write("(") 296 | local parameters = method:GetParameters() 297 | for i = 0, parameters.Length - 1 do 298 | local parameter = parameters[i] 299 | if i ~= 0 then 300 | file:write(", ") 301 | end 302 | local name = get_runtime_type_name_alias(parameter.ParameterType) 303 | local pos = name:find("&") 304 | if pos ~= nil then 305 | name = name:sub(1, pos - 1) 306 | if parameter.IsIn then 307 | name = "in " .. name 308 | elseif parameter.IsOut then 309 | name = "out " .. name 310 | else 311 | name = "ref " .. name 312 | end 313 | else 314 | if parameter.IsIn then 315 | name = "[In] " .. name 316 | end 317 | if parameter.IsOut then 318 | name = "[Out] " .. name 319 | end 320 | end 321 | file:write(name .. " " .. parameter.Name) 322 | local status, err = pcall(function() 323 | if parameter.IsOptional then 324 | local type = parameter.ParameterType 325 | local value = parameter.DefaultValue 326 | if type.IsEnum then 327 | if value.value__ == nil then 328 | file:write(" = 0") 329 | else 330 | file:write(string.format(" = %d", value.value__)) 331 | end 332 | else 333 | if type.FullName == "System.String" then 334 | -- TODO: fix utf-8 encoding 335 | file:write(string.format(" = \"%s\"", value)) 336 | elseif type.FullName == "System.Char" then 337 | file:write(string.format(" = '\\x%X'", value)) 338 | else 339 | file:write(string.format(" = %s", value)) 340 | end 341 | end 342 | end 343 | end) 344 | if not status then 345 | log:write(err .. "\n") 346 | end 347 | end 348 | file:write(") { }") 349 | if rva ~= nil then 350 | file:write(" // RVA: " .. rva) 351 | end 352 | file:write("\n") 353 | if not is_ctor then 354 | local generic_method = method:GetGenericMethodDefinition_impl() 355 | if generic_method ~= nil then 356 | file:write("\t/* GenericMethodDefinition :\n") 357 | file:write("\t |\n") 358 | file:write("\t */\n") 359 | end 360 | end 361 | end 362 | 363 | local flags = CS.System.Reflection.BindingFlags.Instance | -- 364 | CS.System.Reflection.BindingFlags.Static | -- 365 | CS.System.Reflection.BindingFlags.Public | -- 366 | CS.System.Reflection.BindingFlags.NonPublic 367 | 368 | local function do_dump_csharp_type(file, type, index, rvas) 369 | if get_rvas ~= nil then 370 | file:write(string.format("// TypeDefIndex: %d\n", index)) 371 | end 372 | file:write(string.format("// Module: %s\n", type.Module.name)) 373 | local namespace = type.Namespace 374 | if namespace == nil then 375 | file:write("// Namespace:\n") 376 | else 377 | file:write(string.format("// Namespace: %s\n", namespace)) 378 | end 379 | if rvas[1] ~= nil then 380 | file:write("// " .. rvas[1] .. "\n") 381 | end 382 | local index = 2 383 | 384 | local attributes = type:GetCustomAttributes(true) 385 | for i = 0, attributes.Length - 1 do 386 | local text = get_runtime_type_name(attributes[i]:GetType()) 387 | file:write(string.format("[%s]\n", text)) 388 | end 389 | 390 | file:write(get_type_string(type) .. get_runtime_type_name(type, false)) 391 | if not type.IsEnum then 392 | local once = false 393 | local base_type = type.BaseType 394 | if base_type ~= nil then 395 | local name = get_runtime_type_name_alias(base_type) 396 | if name ~= "object" and name ~= "ValueType" then 397 | once = true 398 | end 399 | if once then 400 | file:write(" : " .. name) 401 | end 402 | end 403 | local interfaces = type:GetInterfaces() 404 | if interfaces.Length > 0 then 405 | for i = 0, interfaces.Length - 1 do 406 | local interface = interfaces[i] 407 | local name = get_runtime_type_name_alias(interface) 408 | if interface.FullName ~= nil then 409 | local full = interface.FullName 410 | if base_type ~= nil then 411 | local base_interfaces = base_type:GetInterfaces() 412 | for i = 0, base_interfaces.Length - 1 do 413 | local base_interface = base_interfaces[i] 414 | if base_interface.FullName == interface.FullName then 415 | goto continue 416 | end 417 | end 418 | end 419 | end 420 | if not once then 421 | once = true 422 | file:write(" : " .. name) 423 | else 424 | file:write(", " .. name) 425 | end 426 | ::continue:: 427 | end 428 | end 429 | end 430 | 431 | file:write("\n{") 432 | 433 | local fields = type:GetFields(flags) 434 | if fields.Length > 0 then 435 | local once = false 436 | for j = 0, fields.Length - 1 do 437 | local field = fields[j] 438 | if field.DeclaringType == type then 439 | if not once then 440 | file:write("\n") 441 | file:write("\t// Fields\n") 442 | once = true 443 | end 444 | local attributes = field:GetCustomAttributes(true) 445 | for i = 0, attributes.Length - 1 do 446 | local text = get_runtime_type_name(attributes[i]:GetType()) 447 | file:write(string.format("\t[%s]\n", text)) 448 | end 449 | file:write("\t") 450 | do_dump_csharp_field(file, field) 451 | end 452 | end 453 | end 454 | 455 | local properties = type:GetProperties(flags) 456 | if properties.Length > 0 then 457 | local once = false 458 | for j = 0, properties.Length - 1 do 459 | local property = properties[j] 460 | if property.DeclaringType == type then 461 | if not once then 462 | file:write("\n") 463 | file:write("\t// Properties\n") 464 | once = true 465 | end 466 | local attributes = property:GetCustomAttributes(true) 467 | for i = 0, attributes.Length - 1 do 468 | local text = get_runtime_type_name(attributes[i]:GetType()) 469 | file:write(string.format("\t[%s]\n", text)) 470 | end 471 | file:write("\t") 472 | do_dump_csharp_property(file, property) 473 | end 474 | end 475 | end 476 | 477 | local constructors = type:GetConstructors(flags) 478 | if constructors.Length > 0 then 479 | local once = false 480 | for j = 0, constructors.Length - 1 do 481 | local constructor = constructors[j] 482 | if constructor.DeclaringType == type then 483 | if not once then 484 | file:write("\n") 485 | file:write("\t// Constructors\n") 486 | once = true 487 | end 488 | local attributes = constructor:GetCustomAttributes(true) 489 | for i = 0, attributes.Length - 1 do 490 | local text = get_runtime_type_name(attributes[i]:GetType()) 491 | file:write(string.format("\t[%s]\n", text)) 492 | end 493 | file:write("\t") 494 | do_dump_csharp_method(file, type, constructor, rvas[index], true) 495 | index = index + 1 496 | end 497 | end 498 | end 499 | 500 | local methods = type:GetMethods(flags) 501 | if methods.Length > 0 then 502 | local once = false 503 | for j = 0, methods.Length - 1 do 504 | local method = methods[j] 505 | if method.DeclaringType == type then 506 | if not once then 507 | file:write("\n") 508 | file:write("\t// Methods\n") 509 | once = true 510 | end 511 | local attributes = method:GetCustomAttributes(true) 512 | for i = 0, attributes.Length - 1 do 513 | local text = get_runtime_type_name(attributes[i]:GetType()) 514 | file:write(string.format("\t[%s]\n", text)) 515 | end 516 | file:write("\t") 517 | do_dump_csharp_method(file, type, method, rvas[index], false) 518 | index = index + 1 519 | end 520 | end 521 | end 522 | 523 | file:write("}\n") 524 | end 525 | 526 | local function do_dump_csharp() 527 | local file = io.open(DUMP_CS_FILE, "w") 528 | 529 | local assemblies = CS.System.AppDomain.CurrentDomain:GetAssemblies() 530 | for i = 0, assemblies.Length - 1 do 531 | local assembly = assemblies[i] 532 | file:write(string.format("// Assembly %d: %s\n", i, assembly:ToString())) 533 | end 534 | 535 | local index = 0 536 | 537 | for i = 0, assemblies.Length - 1 do 538 | local assembly = assemblies[i] 539 | local types = assembly:GetTypes() 540 | log:write(string.format("dumping types in assembly %d: %s, total: %d\n", -- 541 | i, assembly:ToString(), types.Length)) 542 | for j = 0, types.Length - 1 do 543 | local type = types[j] 544 | file:write("\n") 545 | local rvas = {} 546 | while true do 547 | if get_rvas == nil then 548 | break 549 | end 550 | rvas = get_rvas(index) 551 | if rvas[1] ~= "" and rvas[1] ~= "" then 552 | break 553 | end 554 | file:write(string.format("// TypeDefIndex: %d\n", index)) 555 | file:write(string.format("// Module: %s\n", type.Module.name)) 556 | local namespace = type.Namespace 557 | if namespace == nil then 558 | file:write("// Namespace:\n") 559 | else 560 | file:write(string.format("// Namespace: %s\n", namespace)) 561 | end 562 | if rvas[1] == "" then 563 | file:write("internal class \n{}\n") 564 | else 565 | file:write("// " .. table.concat(rvas, ";") .. "\n") 566 | end 567 | file:write("\n") 568 | index = index + 1 569 | end 570 | do_dump_csharp_type(file, type, index, rvas) 571 | index = index + 1 572 | end 573 | end 574 | 575 | file:close() 576 | end 577 | 578 | local function main() 579 | log:write("start dumping csharp to " .. DUMP_CS_FILE .. "\n") 580 | do_dump_csharp() 581 | log:write("dumping csharp done\n") 582 | end 583 | 584 | local function on_error(error) 585 | log:write("dumping csharp failed, error: " .. error .. "\n") 586 | end 587 | 588 | xpcall(main, on_error) 589 | 590 | log:close() 591 | --------------------------------------------------------------------------------