Skip to content

Commit 4d4b99c

Browse files
#89:(v1) Canvas: read/write the right encoding of .canvas files.
1 parent 5d8b73f commit 4d4b99c

File tree

7 files changed

+79
-61
lines changed

7 files changed

+79
-61
lines changed

source/phasereditor/phasereditor.canvas.core/src/phasereditor/canvas/core/CanvasCore.java

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import static java.lang.System.out;
2525

2626
import java.io.ByteArrayInputStream;
27-
import java.io.IOException;
2827
import java.io.InputStream;
2928
import java.nio.charset.Charset;
3029
import java.nio.file.Files;
@@ -85,7 +84,7 @@ public class CanvasCore {
8584
ScriptEngineManager m = new ScriptEngineManager();
8685
scriptEngine = m.getEngineByName("nashorn");
8786
}
88-
87+
8988
public static Double scriptEngineEval(String value) {
9089
try {
9190
return Double.valueOf(value);
@@ -98,7 +97,7 @@ public static Double scriptEngineEval(String value) {
9897
}
9998
}
10099
}
101-
100+
102101
public static String scriptEngineValidate(Object value) {
103102
if (value instanceof String) {
104103
String script = (String) value;
@@ -118,7 +117,7 @@ public static String scriptEngineValidate(Object value) {
118117
}
119118
return null;
120119
}
121-
120+
122121
public static void logError(Exception e) {
123122
e.printStackTrace();
124123
StatusManager.getManager().handle(new Status(IStatus.ERROR, CanvasCore.PLUGIN_ID, e.getMessage(), e));
@@ -234,8 +233,8 @@ public IFile getFile() {
234233

235234
public static List<PrefabReference> findPrefabReferencesInFileContent(Prefab prefab, IFile file) {
236235
CanvasModel canvasModel = new CanvasModel(file);
237-
try (InputStream contents = file.getContents()) {
238-
canvasModel.read(new JSONObject(new JSONTokener(contents)));
236+
try {
237+
canvasModel.read(file);
239238
return findPrefabReferenceInModelContent(prefab, canvasModel.getWorld());
240239
} catch (Exception e) {
241240
logError(e);
@@ -317,8 +316,8 @@ public String getId() {
317316

318317
public static List<IAssetReference> findAssetKeyReferencesInFileContent(IAssetKey assetKey, IFile file) {
319318
CanvasModel canvasModel = new CanvasModel(file);
320-
try (InputStream contents = file.getContents()) {
321-
canvasModel.read(new JSONObject(new JSONTokener(contents)));
319+
try {
320+
canvasModel.read(file);
322321
return findAssetKeyReferenceInModelContent(assetKey, canvasModel.getWorld());
323322
} catch (Exception e) {
324323
logError(e);
@@ -374,8 +373,8 @@ public static List<IAssetReference> findAssetKeyReferenceInModelContent(IAssetKe
374373

375374
public static List<IAssetReference> findAssetReferencesInFileContent(AssetModel asset, IFile file) {
376375
CanvasModel canvasModel = new CanvasModel(file);
377-
try (InputStream contents = file.getContents()) {
378-
canvasModel.read(new JSONObject(new JSONTokener(contents)));
376+
try {
377+
canvasModel.read(file);
379378
return findAssetReferenceInModelContent(asset, canvasModel.getWorld());
380379
} catch (Exception e) {
381380
logError(e);
@@ -420,10 +419,10 @@ public static List<IFile> getCanvasDereivedFiles(IFile canvasFile) {
420419

421420
CanvasModel model = new CanvasModel(canvasFile);
422421
SourceLang lang;
423-
try (InputStream contents = canvasFile.getContents()) {
424-
model.read(new JSONObject(new JSONTokener(contents)));
422+
try {
423+
model.read(canvasFile);
425424
lang = model.getSettings().getLang();
426-
} catch (IOException | CoreException e) {
425+
} catch (Exception e) {
427426
logError(e);
428427
throw new RuntimeException(e);
429428
}

source/phasereditor/phasereditor.canvas.core/src/phasereditor/canvas/core/CanvasFile.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,7 @@ public String toString() {
9595

9696
public CanvasModel newModel() throws Exception {
9797
CanvasModel model = new CanvasModel(_file);
98-
try (InputStream contents = _file.getContents()) {
99-
JSONObject data = new JSONObject(new JSONTokener(contents));
100-
model.read(data);
101-
}
98+
model.read(_file);
10299
return model;
103100
}
104101

source/phasereditor/phasereditor.canvas.core/src/phasereditor/canvas/core/CanvasModel.java

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,17 @@
2222
package phasereditor.canvas.core;
2323

2424
import java.io.ByteArrayInputStream;
25+
import java.io.InputStream;
26+
import java.io.InputStreamReader;
27+
import java.nio.charset.Charset;
2528

2629
import org.eclipse.core.resources.IFile;
30+
import org.eclipse.core.resources.IResource;
2731
import org.eclipse.core.runtime.CoreException;
2832
import org.eclipse.core.runtime.IProgressMonitor;
2933
import org.json.JSONException;
3034
import org.json.JSONObject;
35+
import org.json.JSONTokener;
3136

3237
/**
3338
* @author arian
@@ -71,15 +76,27 @@ public void setType(CanvasType type) {
7176
_type = type;
7277
}
7378

74-
public void read(JSONObject data) {
79+
public void read(IFile file) throws Exception {
80+
try (InputStream contents = file.getContents();) {
81+
String charset = file.getCharset();
82+
if (charset == null) {
83+
charset = "UTF-8";
84+
}
85+
InputStreamReader reader = new InputStreamReader(contents, charset);
86+
JSONObject data = new JSONObject(new JSONTokener(reader));
87+
read(data);
88+
}
89+
}
90+
91+
private void read(JSONObject data) {
7592
// version 1 did not write it explicitly
7693
_version = data.optInt("canvas-version", 1);
7794

7895
{
7996
String name = data.optString("type", CanvasType.GROUP.name());
8097
_type = CanvasType.valueOf(name);
8198
}
82-
99+
83100
_settings.read(data.getJSONObject("settings"));
84101
{
85102
JSONObject data2 = data.optJSONObject("stateSettings");
@@ -164,9 +181,27 @@ public void save(IFile file, IProgressMonitor monitor) throws JSONException, Cor
164181

165182
_world.setAssetTable(new AssetTable(_world));
166183
_world.setPrefabTable(new PrefabTable(_world));
167-
184+
168185
write(data, true);
169186

170-
file.setContents(new ByteArrayInputStream(data.toString(2).getBytes()), true, false, monitor);
187+
Charset charset;
188+
189+
if (file.exists()) {
190+
charset = Charset.forName(file.getCharset());
191+
} else {
192+
charset = Charset.forName("UTF-8");
193+
}
194+
195+
String content = data.toString(2);
196+
197+
ByteArrayInputStream stream = new ByteArrayInputStream(content.getBytes(charset));
198+
199+
if (file.exists()) {
200+
file.setContents(stream, IResource.NONE, monitor);
201+
} else {
202+
file.create(stream, false, monitor);
203+
file.setCharset(charset.name(), monitor);
204+
}
205+
171206
}
172207
}

source/phasereditor/phasereditor.canvas.core/src/phasereditor/canvas/core/Prefab.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,11 @@
2121
// USE OR OTHER DEALINGS IN THE SOFTWARE.
2222
package phasereditor.canvas.core;
2323

24-
import java.io.InputStream;
2524
import java.security.InvalidParameterException;
2625
import java.util.UUID;
2726

2827
import org.eclipse.core.resources.IFile;
2928
import org.json.JSONObject;
30-
import org.json.JSONTokener;
3129

3230
/**
3331
* @author arian
@@ -66,10 +64,10 @@ public JSONObject newInstance() {
6664
public JSONObject newInstance(JSONObject initInfo) {
6765
IFile file = getFile();
6866
CanvasModel model = new CanvasModel(file);
69-
try (InputStream contents = file.getContents()) {
70-
JSONObject data = new JSONObject(new JSONTokener(contents));
71-
72-
model.read(data);
67+
try {
68+
69+
model.read(file);
70+
7371
BaseObjectModel objModel;
7472
if (model.getType() == CanvasType.SPRITE) {
7573
objModel = model.getWorld().findFirstSprite();

source/phasereditor/phasereditor.canvas.ui/src/phasereditor/canvas/ui/CanvasAssetReplacer.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,12 @@
2222
package phasereditor.canvas.ui;
2323

2424
import java.io.ByteArrayInputStream;
25-
import java.io.InputStream;
2625
import java.util.List;
2726

2827
import org.eclipse.core.resources.IFile;
2928
import org.eclipse.core.runtime.IProgressMonitor;
3029
import org.eclipse.ui.IEditorPart;
3130
import org.json.JSONObject;
32-
import org.json.JSONTokener;
3331

3432
import phasereditor.assetpack.core.FindAssetReferencesResult;
3533
import phasereditor.assetpack.core.IAssetKey;
@@ -87,9 +85,7 @@ public void replace_ResourceThread(FindAssetReferencesResult result, IAssetKey k
8785
for (IFile file : result.getFiles()) {
8886
CanvasModel canvasModel = new CanvasModel(file);
8987

90-
try (InputStream contents = file.getContents()) {
91-
canvasModel.read(new JSONObject(new JSONTokener(contents)));
92-
}
88+
canvasModel.read(file);
9389

9490
boolean changed = false;
9591

source/phasereditor/phasereditor.canvas.ui/src/phasereditor/canvas/ui/CanvasUI.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
import java.awt.image.BufferedImage;
2828
import java.io.File;
2929
import java.io.IOException;
30-
import java.io.InputStream;
3130
import java.nio.file.Files;
3231
import java.nio.file.Path;
3332
import java.util.ArrayList;
@@ -63,7 +62,6 @@
6362
import org.eclipse.ui.PlatformUI;
6463
import org.eclipse.ui.statushandlers.StatusManager;
6564
import org.json.JSONObject;
66-
import org.json.JSONTokener;
6765

6866
import com.subshell.snippets.jface.tooltip.tooltipsupport.ICustomInformationControlCreator;
6967
import com.subshell.snippets.jface.tooltip.tooltipsupport.TableViewerInformationProvider;
@@ -470,9 +468,9 @@ public void run() {
470468

471469
javafx.scene.image.Image[] result = new javafx.scene.image.Image[1];
472470

473-
try (InputStream contents = file.getContents()) {
471+
try {
474472
CanvasModel model = new CanvasModel(file);
475-
model.read(new JSONObject(new JSONTokener(contents)));
473+
model.read(file);
476474
GroupControl worldControl = new GroupControl(null, model.getWorld());
477475
GroupNode node = worldControl.getNode();
478476
node.setBackground(

source/phasereditor/phasereditor.canvas.ui/src/phasereditor/canvas/ui/editors/CanvasEditor.java

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323

2424
import static phasereditor.ui.PhaserEditorUI.swtRun;
2525

26-
import java.io.InputStream;
2726
import java.util.Arrays;
2827

2928
import org.eclipse.core.commands.operations.IUndoContext;
@@ -74,7 +73,6 @@
7473
import org.eclipse.ui.part.MultiPageEditorPart;
7574
import org.json.JSONException;
7675
import org.json.JSONObject;
77-
import org.json.JSONTokener;
7876

7977
import javafx.geometry.Point2D;
8078
import phasereditor.canvas.core.CanvasCore;
@@ -196,31 +194,29 @@ protected void setInput(IEditorInput input) {
196194
super.setInput(input);
197195
IFileEditorInput fileInput = (IFileEditorInput) input;
198196
IFile file = fileInput.getFile();
199-
try (InputStream contents = file.getContents();) {
200-
JSONObject data = new JSONObject(new JSONTokener(contents));
201-
_model = new CanvasModel(file);
202-
try {
203-
_model.read(data);
204-
} catch (Exception e) {
205-
e.printStackTrace();
206-
_model = new CanvasModel(file);
207-
Display.getDefault().asyncExec(new Runnable() {
208-
209-
@Override
210-
public void run() {
211-
Shell shell = Display.getDefault().getActiveShell();
212-
MessageDialog.openError(shell, "Error", "The scene data cannot ve loaded.\n" + e.getMessage());
213-
}
214-
});
215-
}
216-
_model.getWorld().addPropertyChangeListener(WorldModel.PROP_STRUCTURE, arg -> {
217-
firePropertyChange(PROP_DIRTY);
218-
});
219-
swtRun(this::updateTitle);
197+
198+
_model = new CanvasModel(file);
199+
200+
try {
201+
_model.read(file);
220202
} catch (Exception e) {
221203
e.printStackTrace();
222-
throw new RuntimeException(e);
204+
_model = new CanvasModel(file);
205+
Display.getDefault().asyncExec(new Runnable() {
206+
207+
@Override
208+
public void run() {
209+
Shell shell = Display.getDefault().getActiveShell();
210+
MessageDialog.openError(shell, "Error", "The scene data cannot ve loaded.\n" + e.getMessage());
211+
}
212+
});
223213
}
214+
215+
_model.getWorld().addPropertyChangeListener(WorldModel.PROP_STRUCTURE, arg -> {
216+
firePropertyChange(PROP_DIRTY);
217+
});
218+
219+
swtRun(this::updateTitle);
224220
}
225221

226222
@Override
@@ -406,7 +402,6 @@ private void afterCreateWidgets() {
406402
private void initPalette() {
407403
_paletteComp.setProject(getEditorInputFile().getProject());
408404
}
409-
410405

411406
private void initContexts() {
412407
getContextService().activateContext(EDITOR_CONTEXT_ID);

0 commit comments

Comments
 (0)