forked from hippolippo/Unity-Mod-Maker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChangeManager.py
More file actions
158 lines (141 loc) · 5.2 KB
/
Copy pathChangeManager.py
File metadata and controls
158 lines (141 loc) · 5.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import CodeManager
import copy
undo_stack = []
redo_stack = []
MAX_UNDO_STACK_SIZE = 0
def undo():
global undo_stack, redo_stack
if len(undo_stack) == 0:
return None
redo_stack.append(undo_stack.pop(-1))
return redo_stack[-1]
def redo():
global undo_stack, redo_stack
if len(redo_stack) == 0:
return None
undo_stack.append(redo_stack.pop(-1))
return undo_stack[-1]
def log_action(mod, clear_redo):
global undo_stack, redo_stack
if clear_redo:
redo_stack = []
undo_stack.append(copy.deepcopy(mod))
def update(mod, code, clear_redo=True):
mod.autosave()
log_action(mod, clear_redo)
if len(undo_stack) > MAX_UNDO_STACK_SIZE != 0:
undo_stack.pop(0)
print("over")
mod_code = mod.get_text()
if mod_code == code:
return
front = 0
# Increment front index until the text isn't the same or you hit the end of one of them
while mod_code[:front] == code[:front] and front < min(len(mod_code), len(code)):
front += 1
front -= 1
back = -1
# Decrement the back index until the text isn't the same or you hit front
while mod_code[back:] == code[back:] and front - min(len(mod_code), len(code)) < back + 1:
back -= 1
back += 1
added = code[front:back]
deleteResults = delete(mod.get_code_lines(), front, len(mod_code) + back - front)
addResults = add(mod, front, added)
if "Locked" in deleteResults or "Locked" in addResults:
return "Locked"
#print(front, len(added), front+len(added))
return front + len(added)
def delete(mod, front, amount):
return [delete_char(mod, front) for i in range(amount)]
def delete_char(code_lines, index):
accumulator = 0
i = -1
while accumulator <= index:
i += 1
if i >= len(code_lines):
return
accumulator += len(code_lines[i].get_text())
accumulator -= len(code_lines[i].get_text())
sub_index = index - accumulator
string = [char for char in code_lines[i].get_text()]
if len(string) == 0:
code_lines.pop(i)
return delete_char(code_lines, index)
string.pop(sub_index)
string = "".join(string)
result = code_lines[i].update_contents(string)
if result == "Success":
return "Success"
if result == "Locked":
return "Locked"
if result == "Delimiter":
# There should never be a delimiter on the edge so this shouldn't have an exception
if code_lines[i - 1].is_locked() or code_lines[i + 1].is_locked():
return "Locked"
result = code_lines[i - 1].update_contents(code_lines[i - 1].get_text() + code_lines[i + 1].get_text())
code_lines[i+1].kill()
return result
return "Error"
def add(mod, front, text):
return [add_char(mod, front + i[0], i[1]) for i in enumerate(text)]
def add_char(mod, index, char):
code_lines = mod.get_code_lines()
accumulator = 0
i = -1
while accumulator <= index:
i += 1
if i >= len(code_lines):
return
accumulator += len(code_lines[i].get_text())
accumulator -= len(code_lines[i].get_text())
sub_index = index - accumulator
if sub_index != 0 and sub_index < len(code_lines[i].get_text()) - 1:
string = [char for char in code_lines[i].get_text()]
string.insert(sub_index, char)
string = "".join(string)
result = code_lines[i].update_contents(string)
return result
if sub_index == 0:
if type(code_lines[i - 1]) is CodeManager.Delimiter and code_lines[i - 1].get_text() != "":
string = [char for char in code_lines[i].get_text()]
string.insert(sub_index, char)
string = "".join(string)
result = code_lines[i].update_contents(string)
return result
before = i - 1
if type(code_lines[before]) is CodeManager.Delimiter:
before -= 1
if code_lines[before].is_locked():
string = [char for char in code_lines[i].get_text()]
string.insert(sub_index, char)
string = "".join(string)
result = code_lines[i].update_contents(string)
return result
string = [char for char in code_lines[before].get_text()]
string.append(char)
string = "".join(string)
result = code_lines[before].update_contents(string)
return result
if type(code_lines[i + 1]) is CodeManager.Delimiter and code_lines[i + 1].get_text() != "":
string = [char for char in code_lines[i].get_text()]
string.insert(sub_index, char)
string = "".join(string)
result = code_lines[i].update_contents(string)
return result
after = i + 1
if type(code_lines[after]) is CodeManager.Delimiter:
after += 1
if not code_lines[i].is_locked():
string = [char for char in code_lines[i].get_text()]
string.insert(sub_index, char)
string = "".join(string)
result = code_lines[i].update_contents(string)
return result
else:
string = [char for char in code_lines[after].get_text()]
string.insert(0, char)
string = "".join(string)
result = code_lines[after].update_contents(string)
return result
return "Error"