Skip to content

Commit

Permalink
Merge pull request #9 from buoutuanzi/LuaAsset
Browse files Browse the repository at this point in the history
添加在 export 和 preview 时反编译 lua 功能
  • Loading branch information
zhangjiequan authored Dec 28, 2023
2 parents ea8c550 + 56527bc commit 1d6efb6
Show file tree
Hide file tree
Showing 96 changed files with 11,289 additions and 4 deletions.
7 changes: 7 additions & 0 deletions AssetStudio/AssetStudio.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,11 @@
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
</ItemGroup>

<ItemGroup>
<Folder Include="LuaDecompile\" />
<Content Include="Dependencies\**">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>

</Project>
2 changes: 1 addition & 1 deletion AssetStudio/AssetsManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,7 @@ private void ReadAssets()
obj = new SpriteAtlas(objectReader);
break;
case ClassIDType.TextAsset:
obj = new TextAsset(objectReader);
obj = TextAssetFactory.Create(objectReader);
break;
case ClassIDType.Texture2D:
obj = new Texture2D(objectReader);
Expand Down
13 changes: 12 additions & 1 deletion AssetStudio/Classes/TextAsset.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,24 @@

namespace AssetStudio
{
public sealed class TextAsset : NamedObject
public class TextAsset : NamedObject
{
public byte[] m_Script;

public TextAsset(ObjectReader reader) : base(reader)
{
m_Script = reader.ReadUInt8Array();
//m_Script = Encoding.UTF8.GetBytes("测试一下");
}

public virtual byte[] GetRawScript()
{
return m_Script;
}

public virtual byte[] GetProcessedScript()
{
return m_Script;
}
}
}
31 changes: 31 additions & 0 deletions AssetStudio/ClassesExt/LuaByte.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AssetStudio
{
internal class LuaByte : TextAsset
{
private LuaByteInfo m_LuaByteInfo;

public LuaByte(ObjectReader reader, LuaByteInfo luaByteInfo) : base(reader)
{
m_LuaByteInfo = luaByteInfo;
}

public override byte[] GetProcessedScript()
{
if (!m_LuaByteInfo.HasDecompiled)
{
LuaDecompileUtils.DecompileLua(m_LuaByteInfo);
}
return m_LuaByteInfo.ProcessedByte;
}

public override byte[] GetRawScript()
{
return m_LuaByteInfo.RawByte;
}
}
}
26 changes: 26 additions & 0 deletions AssetStudio/ClassesExt/TextAssetFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AssetStudio
{
public static class TextAssetFactory
{
public static TextAsset Create(ObjectReader reader)
{
reader.Reset();
reader.ReadAlignedString(); // 跳过文件名
byte[] content = reader.ReadUInt8Array();
bool success = LuaByteParser.TryParseLuaByte(content, out LuaByteInfo luaInfo);
if (success)
{
return new LuaByte(reader, luaInfo);
}
else
{
return new TextAsset(reader);
}
}
}
}
45 changes: 45 additions & 0 deletions AssetStudio/Dependencies/ljd/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
*.lua[co]
*.py[cod]

# C extensions
*.so

# Packages
*.egg
*.egg-info
dist
build
eggs
parts
bin
var
sdist
develop-eggs
.installed.cfg
lib
lib64
__pycache__

# Installer logs
pip-log.txt

# Unit test / coverage reports
.coverage
.tox
nosetests.xml

# Translations
*.mo

# Mr Developer
.mr.developer.cfg
.project
.pydevproject
.*.swp


.vscode




20 changes: 20 additions & 0 deletions AssetStudio/Dependencies/ljd/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
The MIT License (MIT)

Copyright (c) 2013 Andrian Nord

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 changes: 30 additions & 0 deletions AssetStudio/Dependencies/ljd/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# What is new ?

Support decompilation of 64 bit luajit bytecode

支持反编译64位luajit字节码

## Ref

[luajit反编译、解密 - 『软件调试区』 - 吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn](https://www.52pojie.cn/thread-1378796-1-1.html)

# zzwlpx's ReadMe

代码在fork https://github.com/NightNord/ljd 的基础上修改的

主要修正了原版本没有将_read_header函数解析过的header中的flag值带进_read_protoypes。

解决了原代码只能解析带调试信息的luajit字节码,而解析不带调试信息字节码时报错的问题。

此版本可解析luajit2.1.0-beta2的字节码文件。如果要解析其它版本的luajit字节码文件,需要将

luajit源码中的opcode,写进instruction.py 和 code.py


使用说明:python main.py "path of luajit-bytecode"

解释文档:http://www.freebuf.com/column/177810.html

参考:https://github.com/NightNord/ljd
https://github.com/feicong/lua_re/blob/master/lua/lua_re3.md
https://bbs.pediy.com/thread-216800.htm
73 changes: 73 additions & 0 deletions AssetStudio/Dependencies/ljd/README.md.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
LuaJIT raw-bytecode decompiler (LJD)
===

The original name was _ljwthgnd_ as in _LuaJIT 'What The Hell is Going On'
Decompiler_ named under the LuaJIT C sources variable naming convention.


__WARNING!__ This code is nor finished, nor tested yet! There is no even
slightest warranty that resulting code is even near to the original. Use it at
your own risk of the wasted time.

__SECOND WARNING!__ And, BTW, this all is a one huge prototype. Because the
"release" version should be written into lua itself. Because it's cool to
decompile the decompiler - a great test too!

Requirements
---

Python __3.0+__ from python.org


How to use it
---

There is no argument parsing right now, so comment out things in the ```main.py```
script and launch it as in ```main.py path/to/file.luac```

TODO
---

There is a lot of work to do, in the order of priority

0. Logical subexpressions in while statements:
```lua
while x < (xi and 2 or 3) do
print ("Hello crazy world!")
end
```

Logical subexpressions (the subexpressions used as operands in
ariphmetic or comparison operations inside other exrpressions) are
currently supported only for ifs. To support them for whiles and
repeat untils an expression unwarping logic should be moved at the
very beginning. But it won't work without all fixes being done in
a loop unwarping logic. So we need to split that and move the fixes
before expressions before loops before ifs. That's not that easy...

1. AST Mutations:
1. Use the line information (or common sense if there is no line
information) to squash similar expressions into a single expression.

2. Formatting improvements
1. Use the line information (or common sense) to preserve empty lines
and break long statements like in the original code.

This is mostly done, but only in the "common sense" part.

2. Use method-style calls and definitions for tables.

3. Features not supported:
1. GOTO statement (from lua 5.2). All the required functionality is
now in place, but that's rather a low-priority task right now.

2. Local sub-blocks:
```lua
do
...
end
```
These subblocks are not reflected anyhow directly in the bytecode.
The only way to guess them is to watch local variable scopes, which
is simple enough in case of non-stripped bytecode and a bit
harder otherwise.
3 changes: 3 additions & 0 deletions AssetStudio/Dependencies/ljd/gconfig.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

#zzw 20180714 support str encode
gFlagDic = {'strEncode' : 'ascii'}
4 changes: 4 additions & 0 deletions AssetStudio/Dependencies/ljd/ljd/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#
# Copyright (C) 2013 Andrian Nord. See Copyright Notice in main.py
#

4 changes: 4 additions & 0 deletions AssetStudio/Dependencies/ljd/ljd/ast/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#
# Copyright (C) 2013 Andrian Nord. See Copyright Notice in main.py
#

Loading

0 comments on commit 1d6efb6

Please sign in to comment.