Skip to content

Commit

Permalink
Change Webview2 UserData into AppData
Browse files Browse the repository at this point in the history
  • Loading branch information
zbx1425 committed Apr 17, 2021
1 parent 6e3dba1 commit d01cded
Show file tree
Hide file tree
Showing 11 changed files with 50 additions and 95 deletions.
11 changes: 2 additions & 9 deletions BlocklyAtsGui/BlocklyAtsGui.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@
<DependentUpon>FormBugReport.cs</DependentUpon>
</Compile>
<Compile Include="WebView\HttpServer.cs" />
<Compile Include="WebView\WebView2Browser.Reflection.cs" />
<Compile Include="Workspace\CompilerFunction.cs" />
<Compile Include="Workspace\BuildRunConfig.cs" />
<Compile Include="WebView\ExternalBrowser.cs" />
Expand Down Expand Up @@ -196,19 +195,13 @@
<PropertyGroup>
<PostBuildEvent>XCOPY "$(SolutionDir)www" "$(TargetDir)\www\" /E /Y
XCOPY "$(SolutionDir)assets" "$(TargetDir)\" /E /Y
COPY /Y "$(TargetDir)\x86\WebView2Loader.dll" "$(TargetDir)\WebView2Loader.x86.dll"
RMDIR /S /Q "$(TargetDir)\x86"
COPY /Y "$(TargetDir)\x64\WebView2Loader.dll" "$(TargetDir)\WebView2Loader.x64.dll"
RMDIR /S /Q "$(TargetDir)\x64"
COPY /Y "$(TargetDir)\arm64\WebView2Loader.dll" "$(TargetDir)\WebView2Loader.arm64.dll"
RMDIR /S /Q "$(TargetDir)\arm64"
IF $(ConfigurationName)==Release (
COPY /Y "$(SolutionDir)BatsWinApi\Release\batswinapi_x86.dll" "$(TargetDir)\lib"
COPY /Y "$(SolutionDir)BatsWinApi\Release\batswinapi_x64.dll" "$(TargetDir)\lib"
ERASE "$(TargetDir)\WebView2Loader.dll"
RMDIR /S /Q "${TargetDir)\BlocklyATS.exe.WebView2"
DEL "$(TargetDir)\*.xml"
DEL "$(TargetDir)\*.zip"
ERASE "$(TargetDir)\*.xml"
ERASE "$(TargetDir)\*.zip"
"C:\Program Files\7-zip\7z.exe" a BlocklyAts.zip .
)</PostBuildEvent>
</PropertyGroup>
Expand Down
7 changes: 6 additions & 1 deletion BlocklyAtsGui/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,12 @@ static void Main() {
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new FormMain());

PreferenceManager.SavePreference();
if (!PreferenceManager.SavePreference()) {
MessageBox.Show(
I18n.TranslateAllLang("Msg.PreferenceWriteFail"), "Cannot Write Preference",
MessageBoxButtons.OK, MessageBoxIcon.Error
);
}
}
}
}
2 changes: 1 addition & 1 deletion BlocklyAtsGui/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,4 @@
// 可以指定所有值,也可以使用以下所示的 "*" 预置版本号和修订号
// 方法是按如下所示使用“*”: :
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.5.101")]
[assembly: AssemblyVersion("1.0.5.102")]
23 changes: 18 additions & 5 deletions BlocklyAtsGui/System/PreferenceManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ static class PreferenceManager {

public static Preference CurrentPreference;

public static string DataDirectory = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
"zbx1425", "BlocklyAts"
);
public static string PreferencePath = Path.Combine(DataDirectory, "Preference.xml");
public static string WebView2UserDataPath = Path.Combine(DataDirectory, "WebView2UserData");

public static bool FirstStartup = true;

public static void ResetPreference() {
Expand All @@ -20,10 +27,15 @@ public static void ResetPreference() {
};
}

public static bool LoadPreference(string path = "Preference.xml") {
if (File.Exists(path)) {
public static bool LoadPreference() {
try {
if (!Directory.Exists(DataDirectory)) Directory.CreateDirectory(DataDirectory);
} catch {
return false;
}
if (File.Exists(PreferencePath)) {
try {
CurrentPreference = Preference.LoadFromFile(path);
CurrentPreference = Preference.LoadFromFile(PreferencePath);
FirstStartup = false;
} catch {
ResetPreference();
Expand All @@ -37,9 +49,10 @@ public static bool LoadPreference(string path = "Preference.xml") {
}
}

public static bool SavePreference(string path = "Preference.xml") {
public static bool SavePreference() {
try {
CurrentPreference.SaveToFile(path);
if (!Directory.Exists(DataDirectory)) Directory.CreateDirectory(DataDirectory);
CurrentPreference.SaveToFile(PreferencePath);
} catch {
return false;
}
Expand Down
4 changes: 2 additions & 2 deletions BlocklyAtsGui/UserInterface/FormAbout.Designer.cs

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

6 changes: 3 additions & 3 deletions BlocklyAtsGui/UserInterface/FormBugReport.Designer.cs

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

2 changes: 1 addition & 1 deletion BlocklyAtsGui/UserInterface/FormBugReport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ private void llbTwitter_LinkClicked(object sender, LinkLabelLinkClickedEventArgs
}

private void llbEmail_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) {
PlatformFunction.CallBrowser("mailto:feedback@zbx1425.cn?subject=BlocklyAts%20Bug%20Report");
PlatformFunction.CallBrowser("mailto:support@zbx1425.cn?subject=BlocklyAts%20Bug%20Report");
}

private void FormBugReport_Load(object sender, EventArgs e) {
Expand Down
25 changes: 14 additions & 11 deletions BlocklyAtsGui/UserInterface/FormMain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -168,12 +168,8 @@ private async void tsbtnSaveAs_Click(object sender, EventArgs e) {
Title = "Save Workspace"
};
if (sfd.ShowDialog() != DialogResult.OK) return;

var workspaceState = await mainWebBrowser.BkySaveWorkspace();
if (workspaceState == null) return;
currentWorkspace.BlocklyXml = new FPXElement(workspaceState);
currentWorkspace.SaveToFile(sfd.FileName);
updateSaveFileState();

await saveWorkspace(sfd.FileName);
}

private async void tsbtnOpen_Click(object sender, EventArgs e) {
Expand All @@ -192,7 +188,18 @@ private async void tsbtnOpen_Click(object sender, EventArgs e) {
}
}

private async Task saveWorkspace(string path = null) {
if (path == null && string.IsNullOrEmpty(currentWorkspace.SaveFilePath)) return;
var workspaceState = await mainWebBrowser.BkySaveWorkspace();
if (workspaceState == null) return;
currentWorkspace.BlocklyXml = new FPXElement(workspaceState);
currentWorkspace.SaveToFile(path);
updateSaveFileState();
}

private async Task<string> buildAllPlatforms() {
await saveWorkspace(); // Autosave

var luaCode = await mainWebBrowser.BkyExportLua();
var outputList = new List<Tuple<string, string>>();
if (currentWorkspace.Config.ShouldCompilex86) {
Expand Down Expand Up @@ -241,11 +248,7 @@ private async void tsbtnCompile_Click(object sender, EventArgs e) {
}

private async void tsbtnSave_Click(object sender, EventArgs e) {
if (string.IsNullOrEmpty(currentWorkspace.SaveFilePath)) return;
var workspaceState = await mainWebBrowser.BkySaveWorkspace();
if (workspaceState == null) return;
currentWorkspace.BlocklyXml = new FPXElement(workspaceState);
currentWorkspace.SaveToFile();
await saveWorkspace();
}

private async void tsbtnCompileRun_Click(object sender, EventArgs e) {
Expand Down
2 changes: 1 addition & 1 deletion BlocklyAtsGui/WebView/HttpServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ private async Task HandleIncomingConnections(CancellationToken cancelToken) {
resp.StatusCode = 404;
string errorMessageTemplate = @"
<h2>Error: 404 Not Found</h2>
<p>Please inform developer with these information: <a href='mailto:zbx1425@outlook.com'>zbx1425@outlook.com</a></p>
<p>Please inform developer with these information.
<hr/>
<table>
<tr><th>Missing Path</th><td>{0}</td></tr>
Expand Down
55 changes: 0 additions & 55 deletions BlocklyAtsGui/WebView/WebView2Browser.Reflection.cs

This file was deleted.

8 changes: 2 additions & 6 deletions BlocklyAtsGui/WebView/WebView2Browser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,10 @@ partial class WebView2Browser : BaseBrowser {
private WebView2 browser;

public WebView2Browser(string url = "about:blank") {
// Use an alternative path for WebView2 Loader, for a better appearance
// Might remove it if it causes troubles
TryLoadWebview2Loader();

browser = new WebView2();
browser.NavigationCompleted += Browser_DocumentCompleted;

var createTask = CoreWebView2Environment.CreateAsync();
var createTask = CoreWebView2Environment.CreateAsync(null, PreferenceManager.WebView2UserDataPath, null);
createTask.Wait();
environment = createTask.Result;
browser.CoreWebView2Ready += (sender, e) => {
Expand Down Expand Up @@ -86,7 +82,7 @@ private void CoreWebView2_WebResourceRequested(object sender, CoreWebView2WebRes
} else {
string errorMessageTemplate = @"
<h2>Error: 404 Not Found</h2>
<p>Please inform developer with these information: <a href='mailto:zbx1425@outlook.com'>zbx1425@outlook.com</a></p>
<p>Please inform developer with these information.</p>
<hr/>
<table>
<tr><th>Missing Path</th><td>{0}</td></tr>
Expand Down

0 comments on commit d01cded

Please sign in to comment.