Skip to content
This repository has been archived by the owner on Apr 27, 2022. It is now read-only.

Commit

Permalink
Added Single file version for easier embedding
Browse files Browse the repository at this point in the history
  • Loading branch information
Sirithang committed Mar 2, 2018
1 parent d1d15a6 commit e09faed
Show file tree
Hide file tree
Showing 7 changed files with 587 additions and 33 deletions.
80 changes: 80 additions & 0 deletions Assets/MarkdeepViewer/Editor/CreateSingleFileVersion.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
using System.CodeDom;
using System.CodeDom.Compiler;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
using UnityEngine;
using UnityEditor;

public class CreateSingleFileVersion
{
[MenuItem("MarkdeepViewer/Build single file version")]
static void Build()
{
string markdeepContent,
darkStyleCSSContent,
lightStyleCSSContent,
docViewerContent,
inspectorOverrideContent;

if (!FindAssetContent("markdeep.min", out markdeepContent)
|| !FindAssetContent("light_style", out lightStyleCSSContent)
|| !FindAssetContent("dark_style", out darkStyleCSSContent)
|| !FindAssetContent("DocViewerWindow", out docViewerContent)
|| !FindAssetContent("TextInspectorBypass", out inspectorOverrideContent))
return;

StringBuilder fileResult = new StringBuilder();

//This define will tell the system it use the single file version which will trigger copy of the needed file (markdeep, css etc.) to the Library folder
fileResult.Append("#define SINGLE_FILE\n\n");

//we append the using manually instead of trying to discover them from the file, as finding the duplicate would be tricky
//TODO : make it more robust by actually parsing the required using so any change is correctly reflected here
fileResult.Append(
"using UnityEngine;\r\nusing UnityEditor;\r\nusing System;\r\nusing System.IO;\r\nusing System.Timers;\r\nusing System.Reflection;\r\n\n");

int startOfClass = docViewerContent.IndexOf("public class");
int endOfClass = docViewerContent.LastIndexOf('}');

fileResult.Append(docViewerContent.Substring(startOfClass, endOfClass - startOfClass - 1));
fileResult.Append("\n\n");
fileResult.AppendFormat("\tstatic readonly string MARKDEEP_CONTENT = {0};\n", ToLiteral(markdeepContent));
fileResult.AppendFormat("\tstatic readonly string LIGHT_STYLE_CONTENT = {0};\n", ToLiteral(lightStyleCSSContent));
fileResult.AppendFormat("\tstatic readonly string DARK_STYLE_CONTENT = {0};\n", ToLiteral(darkStyleCSSContent));
fileResult.Append("}\n\n");

int startOfInspector = inspectorOverrideContent.IndexOf("[CustomEditor");
fileResult.Append(inspectorOverrideContent.Substring(startOfInspector));

System.IO.File.WriteAllText(Application.dataPath + "/../MarkdeepViewerSingleFile.cs", fileResult.ToString());
}

//return false if couldn't find the asset asked
static bool FindAssetContent(string assetName, out string content)
{
string[] guids = AssetDatabase.FindAssets(assetName);
if (guids.Length == 0)
{
Debug.LogErrorFormat("Couldn't find {0} file in the project", assetName);
content = "";
return false;
}

content = System.IO.File.ReadAllText(AssetDatabase.GUIDToAssetPath(guids[0]).Replace("Assets", Application.dataPath));
return true;
}

private static string ToLiteral(string input)
{
using (var writer = new StringWriter())
{
using (var provider = CodeDomProvider.CreateProvider("CSharp"))
{
provider.GenerateCodeFromExpression(new CodePrimitiveExpression(input), writer, null);
return writer.ToString();
}
}
}
}

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

39 changes: 35 additions & 4 deletions Assets/MarkdeepViewer/Editor/DocViewerWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using System.IO;
using System.Timers;
using System.Reflection;
using UnityEngine.UI;

public class DocViewerWindow : EditorWindow, IHasCustomMenu
{
Expand Down Expand Up @@ -36,8 +35,6 @@ public class DocViewerWindow : EditorWindow, IHasCustomMenu
protected string pathToMarkdeep;
protected string pathToStylesheet;

protected ViewerScriptObject scriptObj;

object previousParent = null;

static public DocViewerWindow Load(string url)
Expand Down Expand Up @@ -93,7 +90,10 @@ public virtual void AddItemsToMenu(GenericMenu menu)
private void OnEnable()
{
InitFieldMapping();

#if SINGLE_FILE
//single file version copy it's own embeded file data into file in the Library folder it will link to.
CopyFiles();
#else
string[] markdeepGUID = AssetDatabase.FindAssets("markdeep.min");
if (markdeepGUID.Length == 0)
{
Expand All @@ -113,7 +113,38 @@ private void OnEnable()
{
pathToStylesheet = AssetDatabase.GUIDToAssetPath(markdeepGUID[0]).Replace("Assets", Application.dataPath);
}
#endif
}

#if SINGLE_FILE
void CopyFiles()
{
string lightpath = Application.dataPath + "/../Library/docmd/light_style.css";
string darkpath = Application.dataPath + "/../Library/docmd/dark_style.css";

pathToMarkdeep = Application.dataPath + "/../Library/docmd/markdeep.min.jscript";
pathToStylesheet = Application.dataPath + "/../Library/docmd/"+
(EditorGUIUtility.isProSkin ? "dark_style" : "light_style") + ".css";

if (!System.IO.Directory.Exists(Application.dataPath + "/../Library/docmd"))
System.IO.Directory.CreateDirectory(Application.dataPath + "/../Library/docmd");

if (!System.IO.File.Exists(pathToMarkdeep))
{
System.IO.File.WriteAllText(pathToMarkdeep, MARKDEEP_CONTENT);
}

if (!System.IO.File.Exists(lightpath))
{
System.IO.File.WriteAllText(lightpath, LIGHT_STYLE_CONTENT);
}

if (!System.IO.File.Exists(darkpath))
{
System.IO.File.WriteAllText(darkpath, DARK_STYLE_CONTENT);
}
}
#endif

bool OnLocationChanged(string url)
{
Expand Down
18 changes: 0 additions & 18 deletions Assets/MarkdeepViewer/Editor/ViewerScriptObject.cs

This file was deleted.

Loading

0 comments on commit e09faed

Please sign in to comment.