-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
216 lines (195 loc) · 7.75 KB
/
Copy pathindex.js
File metadata and controls
216 lines (195 loc) · 7.75 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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
import { Vector } from "core/vector";
import { Blueprint } from "game/blueprint";
import { getBuildingDataFromCode } from "game/building_codes";
import { HUDBlueprintPlacer } from "game/hud/parts/blueprint_placer";
import { enumNotificationType } from "game/hud/parts/notifications";
import { HUDSandboxController } from "game/hud/parts/sandbox_controller";
import { Mod } from "mods/mod";
import { SerializerInternal } from "savegame/serializer_internal";
import { BlueprintPacker } from "./BlueprintPacker";
import META from "./mod.json";
const MODES = { json: "json", pack: "pack" };
const HUDSandboxControllerExt = () => ({
giveBlueprints() {
const SHAPES = [
"CbCbCbRb:CwCwCwCw",
// "6CbCbCbCbCbRb:6CwCwCwCwCwCw", // hex
// "Sb----Sb:CbCbCbCb:--CwCw--", // SI
// "Sb----Sb:3b3b3b3b:--3w3w--", // SI
// "SbSbSbSb:1b1b1b1b:--CwCw--", // SI
];
SHAPES.forEach(shape => {
const store = this.root.hubGoals.storedShapes;
if (!store[shape]) store[shape] = 0;
store[shape] += 1000;
});
},
});
const HUDBlueprintPlacerExt = ({ $old }) => ({
createBlueprintFromBuildings(...args) {
$old.createBlueprintFromBuildings.call(this, ...args);
BPStrings.copyToClipboard(this.currentBlueprint.get(), this.root);
},
pasteBlueprint(...args) {
const blueprint = BPStrings.pasteFromClipboard(this.root);
this.lastBlueprintUsed = blueprint || this.lastBlueprintUsed;
$old.pasteBlueprint.call(this, ...args);
},
});
const SerializerInternalExt = () => ({
deserializeEntityNoPlace(root, payload) {
const staticData = payload.components.StaticMapEntity;
window.assert(staticData, "entity has no static data");
const code = staticData.code;
const data = getBuildingDataFromCode(code);
const metaBuilding = data.metaInstance;
const entity = metaBuilding.createEntity({
root,
origin: Vector.fromSerializedObject(staticData.origin),
rotation: staticData.rotation,
originalRotation: staticData.originalRotation,
rotationVariant: data.rotationVariant,
variant: data.variant,
});
entity.uid = payload.uid;
let errorStatus = this.deserializeComponents(root, entity, payload.components);
return errorStatus || entity;
},
});
class BPStrings extends Mod {
static serializeAsJson(entities) {
const result = [];
// let data = new SerializerInternal().serializeEntityArray(entities);
for (let entity of entities) {
if (entity.queuedForDestroy || entity.destroyed) continue;
const item = entity.serialize();
delete item.uid;
const comps = Object.entries(entity.components);
for (let [name, comp] of comps) {
const obj = {};
comp.copyAdditionalStateTo(obj);
if (name == "StaticMapEntity" || Object.keys(obj).length > 0) continue;
delete item.components[name];
}
result.push(item);
}
return JSON.stringify(result, null, 2);
}
static deserializeJson(root, data) {
const json = JSON.parse(data);
if (typeof json != "object") return;
if (!Array.isArray(json)) return;
const serializer = new SerializerInternal();
/** @type {Array<Entity>} */
const entities = [];
for (let i = 0; i < json.length; ++i) {
/** @type {Entity?} */
const value = json[i];
if (!value.components || !value.components.StaticMapEntity) return;
const staticData = value.components.StaticMapEntity;
if (!staticData.code == undefined || !staticData.origin) return;
const result = serializer.deserializeEntityNoPlace(root, value);
if (typeof result === "string") throw new Error(result);
entities.push(result);
}
return entities;
}
static serialize(entities) {
// console.debug("##### data out:", entities);
const mode = META.settings.mode;
let data = "";
switch (mode) {
case MODES.json:
data = BPStrings.serializeAsJson(entities);
break;
case MODES.pack:
data = new BlueprintPacker().packEntities(entities);
break;
default:
throw `Unknown blueprint string mode: ${mode}`;
}
return data;
}
static deserialize(root, data) {
const EOL = "\n";
let entities;
try {
entities = BPStrings.deserializeJson(root, data);
if (!entities) throw "Unable to parse blueprint string as JSON";
} catch (e) {
data = data
.split(EOL)
.map(s => s.trim())
.join("");
entities = new BlueprintPacker().unpackEntities(root, data);
}
// console.debug("##### data in:", entities);
return entities;
}
static async copyToClipboard(blueprint, root) {
try {
const data = BPStrings.serialize(blueprint.entities);
console.debug("Copy to clipboard:", data);
await navigator.clipboard.writeText(data);
// this.root.soundProxy.playUi(SOUNDS.copy);
// console.debug("Blueprint copied to clipboard");
root.hud.signals.notification.dispatch(
"Blueprint copied to clipboard",
enumNotificationType.info
);
} catch (e) {
console.error("Copy to clipboard failed:", e);
}
}
static pasteFromClipboard(root) {
let data;
let blueprint;
try {
data = BPStrings.getClipboard().trim();
// replace all with plain space
// data = data.replace(/\xA0/g, " ");
console.debug("Received data from clipboard:", data);
const entities = BPStrings.deserialize(root, data);
if (!entities) throw "Unable to parse blueprint string";
blueprint = new Blueprint(entities);
} catch (e) {
console.error("Paste from clipboard failed:", e);
}
if (blueprint) {
root.hud.signals.notification.dispatch(
"Received blueprint from clipboard",
enumNotificationType.info
);
}
return blueprint;
}
static getClipboard() {
const pasteTarget = document.createElement("textarea");
pasteTarget.setAttribute("position", "absolute");
pasteTarget.setAttribute("height", "0");
pasteTarget.setAttribute("overflow", "hidden");
pasteTarget.setAttribute("autocomplete", "off");
pasteTarget.setAttribute("autocorrect", "off");
pasteTarget.setAttribute("spellcheck", "false");
const actElem = document.activeElement.appendChild(pasteTarget).parentNode;
pasteTarget.focus();
document.execCommand("Paste", null, null);
const value = pasteTarget.value;
actElem.removeChild(pasteTarget);
return value;
}
// for debugging
initSandbox() {
this.modInterface.registerHudElement("sandboxController", HUDSandboxController);
this.modInterface.extendClass(HUDSandboxController, HUDSandboxControllerExt);
}
init() {
console.debug("##### Init mod:", this.metadata.id);
META.settings = this.settings; // Get saved settings
// this.initSandbox();
this.modInterface.extendClass(SerializerInternal, SerializerInternalExt);
this.modInterface.extendClass(HUDBlueprintPlacer, HUDBlueprintPlacerExt);
}
}
// eslint-disable-next-line no-undef
registerMod(BPStrings, META);