Skip to content

Commit d1e0221

Browse files
committed
Add a parameter to exclude debug instructions from reassembled SWF
1 parent 399dd88 commit d1e0221

File tree

7 files changed

+60
-26
lines changed

7 files changed

+60
-26
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ Native/BytecodeEditor/BytecodeEditor.vcxproj.user
1010
*.dll
1111
AS3/bin
1212
AS3/obj
13+
*.ane

ANEBytecodeEditor.ane

-287 KB
Binary file not shown.

AS3/src/com/cff/anebe/BytecodeEditor.as

+5-5
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ package com.cff.anebe
9393

9494
// Assembles an SWF from a map of file name to file contents. If replaceSWF is unspecified, and Disassemble was called, uses
9595
// the last SWF's data for the rest of the SWF.
96-
public function Assemble(strings:Object, replaceSWF:ByteArray = null):ByteArray
96+
public function Assemble(strings:Object, includeDebugInstructions:Boolean, replaceSWF:ByteArray = null):ByteArray
9797
{
9898
if (replaceSWF != null)
9999
{
@@ -155,7 +155,7 @@ package com.cff.anebe
155155
vec[vec.length] = str;
156156
}
157157

158-
ret = extContext.call("Assemble", strings, vec);
158+
ret = extContext.call("Assemble", strings, vec, includeDebugInstructions);
159159

160160
if (ret == null)
161161
{
@@ -248,7 +248,7 @@ package com.cff.anebe
248248

249249
// Assembles an SWF from a map of file name to file contents, asynchronously. If replaceSWF is unspecified, and Disassemble was called, uses
250250
// the last SWF's data for the rest of the SWF.
251-
public function AssembleAsync(strings:Object, replaceSWF:ByteArray = null):void
251+
public function AssembleAsync(strings:Object, includeDebugInstructions:Boolean, replaceSWF:ByteArray = null):void
252252
{
253253
if (replaceSWF != null)
254254
{
@@ -310,7 +310,7 @@ package com.cff.anebe
310310
vec[vec.length] = str;
311311
}
312312

313-
ret = extContext.call("AssembleAsync", strings, vec);
313+
ret = extContext.call("AssembleAsync", strings, vec, includeDebugInstructions);
314314

315315
if (ret == null)
316316
{
@@ -364,4 +364,4 @@ package com.cff.anebe
364364
}
365365
}
366366

367-
}
367+
}

Native/BytecodeEditor/include/Assembler.hpp

+15-4
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ class Assembler
2828
{
2929
private:
3030
const std::unordered_map<std::string, std::string>& strings;
31+
bool includeDebugInstructions;
3132

3233
static constexpr char addUniqueMethod[] = "method";
3334
static constexpr char addUniqueClass[] = "class";
@@ -1379,7 +1380,12 @@ class Assembler
13791380
OPCode(uint8_t(OPCode::OP_setlocal0) + instruction.arguments[0].uintv());
13801381
}
13811382

1382-
ret.emplace_back(std::move(instruction));
1383+
if (includeDebugInstructions || (instruction.opcode != OPCode::OP_debug &&
1384+
instruction.opcode != OPCode::OP_debugfile &&
1385+
instruction.opcode != OPCode::OP_debugline))
1386+
{
1387+
ret.emplace_back(std::move(instruction));
1388+
}
13831389
}
13841390

13851391
for (const auto& f : jumpFixups)
@@ -1471,7 +1477,11 @@ class Assembler
14711477
}
14721478
}
14731479

1474-
Assembler(const std::unordered_map<std::string, std::string>& strings) : strings(strings) {}
1480+
Assembler(
1481+
const std::unordered_map<std::string, std::string>& strings, bool includeDebugInstructions)
1482+
: strings(strings), includeDebugInstructions(includeDebugInstructions)
1483+
{
1484+
}
14751485

14761486
ASASM::ASProgram readProgram()
14771487
{
@@ -1565,14 +1575,15 @@ class Assembler
15651575
}
15661576

15671577
public:
1568-
static ASASM::ASProgram assemble(const std::unordered_map<std::string, std::string>& strings)
1578+
static ASASM::ASProgram assemble(
1579+
const std::unordered_map<std::string, std::string>& strings, bool includeDebugInstructions)
15691580
{
15701581
if (!strings.contains("main.asasm"))
15711582
{
15721583
throw StringException("Assembly start (main.asasm) not found");
15731584
}
15741585

1575-
Assembler assembler(strings);
1586+
Assembler assembler(strings, includeDebugInstructions);
15761587

15771588
std::shared_ptr<SourceFile> mainFile =
15781589
std::make_shared<SourceFile>("main.asasm", strings.at("main.asasm"));

Native/BytecodeEditor/include/BytecodeEditor.hpp

+4-3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <thread>
99
#include <unordered_map>
1010
#include <variant>
11+
#include <vector>
1112

1213
class BytecodeEditor
1314
{
@@ -28,8 +29,8 @@ class BytecodeEditor
2829
FREObject disassemble();
2930
FREObject disassembleAsync();
3031

31-
FREObject assemble(std::unordered_map<std::string, std::string>&& data);
32-
FREObject assembleAsync(std::unordered_map<std::string, std::string>&& data);
32+
FREObject assemble(std::unordered_map<std::string, std::string>&& data, bool includeDebugInstructions);
33+
FREObject assembleAsync(std::unordered_map<std::string, std::string>&& data, bool includeDebugInstructions);
3334

3435
void setSWF(SWF::SWFFile&& file) { currentSWF = std::move(file); }
3536

@@ -39,7 +40,7 @@ class BytecodeEditor
3940
{
4041
runningTask.join();
4142
}
42-
currentSWF = std::nullopt;
43+
currentSWF = std::nullopt;
4344
m_taskResult = std::monostate{};
4445
}
4546

Native/BytecodeEditor/source/ANEBytecodeEditor.cpp

+24-6
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ FREObject Disassemble(FREContext ctx, void* funcData, uint32_t argc, FREObject a
3131

3232
FREObject Assemble(FREContext ctx, void* funcData, uint32_t argc, FREObject argv[])
3333
{
34-
if (argc != 2)
34+
if (argc != 3)
3535
{
36-
FAIL("argc should be 2");
36+
FAIL("argc should be 3");
3737
}
3838

3939
GET_EDITOR(ctx);
@@ -49,6 +49,11 @@ FREObject Assemble(FREContext ctx, void* funcData, uint32_t argc, FREObject argv
4949
{
5050
FAIL("argv[1] is not a vector");
5151
}
52+
DO_OR_FAIL("Failed to get argv[2] type", FREGetObjectType(argv[2], &type));
53+
if (type != FRE_TYPE_BOOLEAN)
54+
{
55+
FAIL("argv[2] is not a boolean");
56+
}
5257

5358
uint32_t vecSize;
5459
DO_OR_FAIL("Failed to get argv[1] size", FREGetArrayLength(argv[1], &vecSize));
@@ -72,7 +77,11 @@ FREObject Assemble(FREContext ctx, void* funcData, uint32_t argc, FREObject argv
7277
strings.emplace(key, val);
7378
}
7479

75-
return editor->assemble(std::move(strings));
80+
uint32_t includeDebugInstructions;
81+
DO_OR_FAIL("Failed to get argv[2]'s boolean value",
82+
FREGetObjectAsBool(argv[2], &includeDebugInstructions));
83+
84+
return editor->assemble(std::move(strings), includeDebugInstructions != 0);
7685
}
7786

7887
FREObject DisassembleAsync(FREContext ctx, void* funcData, uint32_t argc, FREObject argv[])
@@ -89,9 +98,9 @@ FREObject DisassembleAsync(FREContext ctx, void* funcData, uint32_t argc, FREObj
8998

9099
FREObject AssembleAsync(FREContext ctx, void* funcData, uint32_t argc, FREObject argv[])
91100
{
92-
if (argc != 2)
101+
if (argc != 3)
93102
{
94-
FAIL("argc should be 2");
103+
FAIL("argc should be 3");
95104
}
96105

97106
GET_EDITOR(ctx);
@@ -107,6 +116,11 @@ FREObject AssembleAsync(FREContext ctx, void* funcData, uint32_t argc, FREObject
107116
{
108117
FAIL("argv[1] is not a vector");
109118
}
119+
DO_OR_FAIL("Failed to get argv[2] type", FREGetObjectType(argv[2], &type));
120+
if (type != FRE_TYPE_BOOLEAN)
121+
{
122+
FAIL("argv[2] is not a boolean");
123+
}
110124

111125
uint32_t vecSize;
112126
DO_OR_FAIL("Failed to get argv[1] size", FREGetArrayLength(argv[1], &vecSize));
@@ -130,7 +144,11 @@ FREObject AssembleAsync(FREContext ctx, void* funcData, uint32_t argc, FREObject
130144
strings.emplace(key, val);
131145
}
132146

133-
return editor->assembleAsync(std::move(strings));
147+
uint32_t includeDebugInstructions;
148+
DO_OR_FAIL("Failed to get argv[2]'s boolean value",
149+
FREGetObjectAsBool(argv[2], &includeDebugInstructions));
150+
151+
return editor->assembleAsync(std::move(strings), includeDebugInstructions != 0);
134152
}
135153

136154
FREObject AsyncTaskResult(FREContext ctx, void* funcData, uint32_t argc, FREObject argv[])

Native/BytecodeEditor/source/BytecodeEditor.cpp

+11-8
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ FREObject BytecodeEditor::disassemble()
5151
}
5252
}
5353

54-
FREObject BytecodeEditor::assemble(std::unordered_map<std::string, std::string>&& strings)
54+
FREObject BytecodeEditor::assemble(
55+
std::unordered_map<std::string, std::string>&& strings, bool includeDebugInstructions)
5556
{
5657
if (runningTask.joinable())
5758
{
@@ -62,8 +63,8 @@ FREObject BytecodeEditor::assemble(std::unordered_map<std::string, std::string>&
6263
{
6364
FAIL("No SWF data specified to reassemble");
6465
}
65-
std::vector<uint8_t> data =
66-
std::move(ABC::ABCWriter(Assembler::assemble(strings).toABC()).data());
66+
std::vector<uint8_t> data = std::move(
67+
ABC::ABCWriter(Assembler::assemble(strings, includeDebugInstructions).toABC()).data());
6768

6869
currentSWF->replaceABCData(data.data(), data.size());
6970

@@ -117,22 +118,24 @@ FREObject BytecodeEditor::disassembleAsync()
117118
return ret;
118119
}
119120

120-
FREObject BytecodeEditor::assembleAsync(std::unordered_map<std::string, std::string>&& strings)
121+
FREObject BytecodeEditor::assembleAsync(
122+
std::unordered_map<std::string, std::string>&& strings, bool includeDebugInstructions)
121123
{
122124
if (runningTask.joinable())
123125
{
124126
FAIL("Already running a task");
125127
}
126128

127-
runningTask = std::jthread([this, strings = std::move(strings)] {
129+
runningTask = std::jthread([this, strings = std::move(strings), includeDebugInstructions] {
128130
if (!currentSWF)
129131
{
130132
FAIL_ASYNC("No SWF data specified to reassemble");
131133
}
132134
try
133135
{
134-
std::vector<uint8_t> data =
135-
std::move(ABC::ABCWriter(Assembler::assemble(strings).toABC()).data());
136+
std::vector<uint8_t> data = std::move(
137+
ABC::ABCWriter(Assembler::assemble(strings, includeDebugInstructions).toABC())
138+
.data());
136139

137140
currentSWF->replaceABCData(data.data(), data.size());
138141

@@ -225,4 +228,4 @@ FREObject BytecodeEditor::taskResult()
225228
default:
226229
FAIL("Invalid task result index");
227230
}
228-
}
231+
}

0 commit comments

Comments
 (0)