Skip to content

Commit

Permalink
zip write works
Browse files Browse the repository at this point in the history
  • Loading branch information
n1ckfg committed Jun 26, 2018
1 parent 15d7952 commit a487838
Show file tree
Hide file tree
Showing 23 changed files with 21,462 additions and 21,785 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
/[Bb]uild/
/[Bb]uilds/
/Assets/AssetStoreTools*
/.vs/

# Autogenerated VS/MD solution and project files
ExportedObj/
Expand Down
7,158 changes: 3,579 additions & 3,579 deletions Example/Animations/layer_test.json

Large diffs are not rendered by default.

Binary file modified Example/Animations/layer_test.latk
Binary file not shown.
35,042 changes: 17,521 additions & 17,521 deletions Example/Animations/transform_test.json

Large diffs are not rendered by default.

Binary file added Example/Animations/transform_test.latk
Binary file not shown.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

274 changes: 274 additions & 0 deletions Example/Animations/untitled.json

Large diffs are not rendered by default.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file modified Example/Animations/untitled.latk
Binary file not shown.
6 changes: 5 additions & 1 deletion Example/Scenes/Example.unity
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ Prefab:
objectReference: {fileID: 387278315}
- target: {fileID: 11437508, guid: bd6b520ae512fe14284192c59b70f0f3, type: 2}
propertyPath: readFileName
value: latkUnity/Example/Animations/layer_test.json
value: latkUnity/Example/Animations/layer_test.latk
objectReference: {fileID: 0}
- target: {fileID: 11437508, guid: bd6b520ae512fe14284192c59b70f0f3, type: 2}
propertyPath: readOnStart
Expand Down Expand Up @@ -234,6 +234,10 @@ Prefab:
propertyPath: brushMat.Array.data[0]
value:
objectReference: {fileID: 2100000, guid: 7ff5f226683f27a4f8e5be4f5a466cba, type: 2}
- target: {fileID: 11437508, guid: bd6b520ae512fe14284192c59b70f0f3, type: 2}
propertyPath: writeFileName
value: current.latk
objectReference: {fileID: 0}
m_RemovedComponents: []
m_ParentPrefab: {fileID: 100100000, guid: bd6b520ae512fe14284192c59b70f0f3, type: 2}
m_IsPrefabParent: 0
Expand Down
105 changes: 79 additions & 26 deletions Scripts/LightningArtist.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,6 @@ public enum FillEmptyMethod { NONE, WRITE, DISPLAY };
private Matrix4x4 transformMatrix;
private Matrix4x4 cameraTransformMatrix;

private bool useZip = false;

public void updateTransformMatrix() {
transformMatrix = Matrix4x4.TRS(transform.position, transform.rotation, transform.localScale);
}
Expand Down Expand Up @@ -453,11 +451,7 @@ public IEnumerator readLatkStrokes() {

string ext = Path.GetExtension(readFileName).ToLower();
Debug.Log("Found extension " + ext);
if (ext == ".latk" || ext == ".zip") {
useZip = true;
} else {
useZip = false;
}
bool useZip = (ext == ".latk" || ext == ".zip");

for (int h = 0; h < layerList.Count; h++) {
for (int i = 0; i < layerList[h].frameList.Count; i++) {
Expand Down Expand Up @@ -582,6 +576,10 @@ public IEnumerator writeLatkStrokes() {
Debug.Log("*** Begin writing...");
isWritingFile = true;

string ext = Path.GetExtension(writeFileName).ToLower();
Debug.Log("Found extension " + ext);
bool useZip = (ext == ".latk" || ext == ".zip");

List<string> FINAL_LAYER_LIST = new List<string>();

for (int hh = 0; hh < layerList.Count; hh++) {
Expand Down Expand Up @@ -691,33 +689,35 @@ public IEnumerator writeLatkStrokes() {
s.Add("}");

string url = "";
string tempName = "";
if (useTimestamp) {
string ext = ".json";
string tempName = writeFileName.Replace(ext, "");
string extO = "";
if (useZip) {
extO = ".latk";
} else {
extO = ".json";
}
tempName = writeFileName.Replace(extO, "");
int timestamp = (int)(System.DateTime.UtcNow.Subtract(new System.DateTime(1970, 1, 1))).TotalSeconds;
tempName += "_" + timestamp + ext;
url = Path.Combine(Application.dataPath, tempName);
tempName += "_" + timestamp + extO;
}

#if UNITY_ANDROID
url = "/sdcard/Movies/" + tempName;
#endif
url = Path.Combine(Application.dataPath, tempName);

#if UNITY_IOS
url = Path.Combine(Application.persistentDataPath, tempName);
#endif
} else {
url = Path.Combine(Application.dataPath, writeFileName);
#if UNITY_ANDROID
url = "/sdcard/Movies/" + tempName;
#endif

#if UNITY_ANDROID
url = "/sdcard/Movies/" + writeFileName;
#endif
#if UNITY_IOS
url = Path.Combine(Application.persistentDataPath, tempName);
#endif

#if UNITY_IOS
url = Path.Combine(Application.persistentDataPath, writeFileName);
#endif
if (useZip) {
saveJsonAsZip(url, tempName, string.Join("\n", s.ToArray()));
} else {
File.WriteAllText(url, string.Join("\n", s.ToArray()));
}

File.WriteAllText(url, string.Join("\n", s.ToArray()));
Debug.Log("*** Wrote " + url);
isWritingFile = false;

Expand Down Expand Up @@ -1045,4 +1045,57 @@ JSONNode getJsonFromZip(byte[] bytes) {
return null;
}

void saveJsonAsZip(string url, string fileName, string s) {
// https://stackoverflow.com/questions/1879395/how-do-i-generate-a-stream-from-a-string
// https://github.com/icsharpcode/SharpZipLib/wiki/Zip-Samples
// https://stackoverflow.com/questions/8624071/save-and-load-memorystream-to-from-a-file

MemoryStream memStreamIn = new MemoryStream();
StreamWriter writer = new StreamWriter(memStreamIn);
writer.Write(s);
writer.Flush();
memStreamIn.Position = 0;

MemoryStream outputMemStream = new MemoryStream();
ZipOutputStream zipStream = new ZipOutputStream(outputMemStream);

zipStream.SetLevel(3); //0-9, 9 being the highest level of compression

string fileNameMinusExtension = "";
string[] nameTemp = fileName.Split('.');
for (int i = 0; i < nameTemp.Length - 1; i++) {
fileNameMinusExtension += nameTemp[i];
}

ZipEntry newEntry = new ZipEntry(fileNameMinusExtension + ".json");
newEntry.DateTime = System.DateTime.Now;

zipStream.PutNextEntry(newEntry);

StreamUtils.Copy(memStreamIn, zipStream, new byte[4096]);
zipStream.CloseEntry();

zipStream.IsStreamOwner = false; // False stops the Close also Closing the underlying stream.
zipStream.Close(); // Must finish the ZipOutputStream before using outputMemStream.

outputMemStream.Position = 0;

using (FileStream file = new FileStream(url, FileMode.Create, System.IO.FileAccess.Write)) {
byte[] bytes = new byte[outputMemStream.Length];
outputMemStream.Read(bytes, 0, (int)outputMemStream.Length);
file.Write(bytes, 0, bytes.Length);
outputMemStream.Close();
}

/*
// Alternative outputs:
// ToArray is the cleaner and easiest to use correctly with the penalty of duplicating allocated memory.
byte[] byteArrayOut = outputMemStream.ToArray();
// GetBuffer returns a raw buffer raw and so you need to account for the true length yourself.
byte[] byteArrayOut = outputMemStream.GetBuffer();
long len = outputMemStream.Length;
*/
}

}
Loading

0 comments on commit a487838

Please sign in to comment.