From b934378758cb0d366488395e3e929d98457946d6 Mon Sep 17 00:00:00 2001 From: Brian Fiete Date: Sat, 5 Oct 2019 10:24:58 -0700 Subject: [PATCH] Fixed some hash checking errors --- BeefLibs/Beefy2D/src/Utils.bf | 32 +++++++++++++++-- BeefLibs/corlib/src/Text/Encoding.bf | 23 +++++++++++- IDE/src/IDEApp.bf | 52 ++++++++++++++++++++-------- 3 files changed, 89 insertions(+), 18 deletions(-) diff --git a/BeefLibs/Beefy2D/src/Utils.bf b/BeefLibs/Beefy2D/src/Utils.bf index e3bdde0f..4be4bd5d 100644 --- a/BeefLibs/Beefy2D/src/Utils.bf +++ b/BeefLibs/Beefy2D/src/Utils.bf @@ -159,28 +159,54 @@ namespace Beefy public static Result LoadTextFile(String fileName, String outBuffer, bool autoRetry = true, delegate void() onPreFilter = null) { + FileStream sr = scope .(); + + // Retry for a while if the other side is still writing out the file for (int i = 0; i < 100; i++) { - if (File.ReadAllText(fileName, outBuffer, true) case .Err(let err)) + if (sr.Open(fileName) case .Err(let fileOpenErr)) { bool retry = false; - if ((autoRetry) && (err case .FileOpenError(let fileOpenErr))) + if (autoRetry) { if (fileOpenErr == .SharingViolation) retry = true; } if (!retry) - return .Err(err); + return .Err(.FileOpenError(fileOpenErr)); } else break; Thread.Sleep(20); } + int fileLen = sr.Length; + if (sr.TryRead(.((.)outBuffer.PrepareBuffer(fileLen), fileLen)) case .Err(let readErr)) + return .Err(.FileReadError(readErr)); + if (onPreFilter != null) onPreFilter(); + int startLen = Math.Min(fileLen, 4); + Span bomSpan = .((.)outBuffer.Ptr, startLen); + var encoding = Encoding.DetectEncoding(bomSpan, var bomSize); + if (bomSize > 0) + { + if (encoding == .UTF8WithBOM) + { + outBuffer.Remove(0, bomSize); + } + else + { + String srcBuffer = scope .(); + outBuffer.MoveTo(srcBuffer); + Span inSpan = .((.)srcBuffer.Ptr, srcBuffer.Length); + inSpan.RemoveFromStart(bomSize); + encoding.DecodeToUTF8(inSpan, outBuffer).IgnoreError(); + } + } + /*if (hashPtr != null) *hashPtr = MD5.Hash(Span((uint8*)outBuffer.Ptr, outBuffer.Length));*/ diff --git a/BeefLibs/corlib/src/Text/Encoding.bf b/BeefLibs/corlib/src/Text/Encoding.bf index 986c1737..a0ab5f0c 100644 --- a/BeefLibs/corlib/src/Text/Encoding.bf +++ b/BeefLibs/corlib/src/Text/Encoding.bf @@ -59,6 +59,27 @@ namespace System.Text /// Decodes from bytes to UTF8 public abstract Result DecodeToUTF8(Span inBytes, StringView outChars); + /// Decodes from bytes to UTF8 + public virtual Result DecodeToUTF8(Span inBytes, String outStr) + { + int utf8Len = GetDecodedUTF8Size(inBytes); + + int prevSize = outStr.Length; + switch (DecodeToUTF8(inBytes, StringView(outStr.PrepareBuffer(utf8Len)))) + { + case .Ok(let val): + return .Ok(val); + case .Err(let err): + switch (err) + { + case .PartialDecode(let decodedBytes, let outChars): + outStr.[Friend]mLength = (.)(prevSize + outChars); + case .FormatError: + } + return .Err(err); + } + } + public static Encoding DetectEncoding(Span data, out int bomSize) { bomSize = 0; @@ -261,7 +282,7 @@ namespace System.Text public override int GetDecodedUTF8Size(Span bytes) { - return Text.UTF16.GetLengthAsUTF8(Span((.)bytes.Ptr, bytes.Length)); + return Text.UTF16.GetLengthAsUTF8(Span((.)bytes.Ptr, bytes.Length / 2)); } public override Result DecodeToUTF8(Span inBytes, StringView outChars) diff --git a/IDE/src/IDEApp.bf b/IDE/src/IDEApp.bf index c9f59491..0c491220 100644 --- a/IDE/src/IDEApp.bf +++ b/IDE/src/IDEApp.bf @@ -123,6 +123,7 @@ namespace IDE public String mDbgCompileDir ~ delete _; public String mDbgVersionedCompileDir ~ delete _; public DateTime mDbgHighestTime; + public bool mForceFirstRun; public bool mIsFirstRun; public int? mTargetExitCode; public FileVersionInfo mVersionInfo ~ delete _; @@ -575,7 +576,7 @@ namespace IDE public ~this() { #if !CLI - if (!mStartedWithTestScript) + if (!mStartedWithTestScript && !mForceFirstRun) { mSettings.Save(); SaveDefaultLayoutData(); @@ -5376,7 +5377,18 @@ namespace IDE char8 c = text[i]; if (c == '\r') { - if ((i < text.Length - 1) && (text[i + 1] == '\n')) + char8 nextC = 0; + if (i < text.Length - 1) + { + nextC = text[++i]; + if (nextC == 0) + { + if (i < text.Length - 2) + nextC = text[++i]; + } + } + + if (nextC == '\n') editData.mLineEndingKind = .CrLf; else editData.mLineEndingKind = .Cr; @@ -6311,6 +6323,9 @@ namespace IDE mVerb = .New; case "-testNoExit": mExitWhenTestScriptDone = false; + case "-firstRun": + mForceFirstRun = true; + mIsFirstRun = true; case "-clean": mWantsClean = true; case "-dbgCompileDump": @@ -8156,6 +8171,13 @@ namespace IDE if (doCompile) { + for (var project in mWorkspace.mProjects) + { + // Regenerate these + DeleteContainerAndItems!(project.mCurBfOutputFileNames); + project.mCurBfOutputFileNames = null; + } + var dir = scope String(); GetWorkspaceBuildDir(dir); bfCompiler.QueueCompile(dir); @@ -9707,7 +9729,7 @@ namespace IDE //TODO: //mConfigName.Set("Dbg"); - if ((!mRunningTestScript) && (LoadDefaultLayoutData())) + if ((!mRunningTestScript) && (!mIsFirstRun) && (LoadDefaultLayoutData())) { return; } @@ -9716,8 +9738,8 @@ namespace IDE TabbedView projectTabbedView = CreateTabbedView(); SetupTab(projectTabbedView, "Workspace", 0, mProjectPanel, false); - projectTabbedView.SetRequestedSize(200, 200); - projectTabbedView.mWidth = 200; + projectTabbedView.SetRequestedSize(GS!(200), GS!(200)); + projectTabbedView.mWidth = GS!(200); //TabbedView propertiesView = CreateTabbedView(); //propertiesView.AddTab("Properties", 0, mPropertiesPanel, false); @@ -9732,14 +9754,14 @@ namespace IDE var outputTabbedView = CreateTabbedView(); mDockingFrame.AddDockedWidget(outputTabbedView, null, DockingFrame.WidgetAlign.Bottom); - outputTabbedView.SetRequestedSize(250, 250); + outputTabbedView.SetRequestedSize(GS!(250), GS!(250)); - SetupTab(outputTabbedView, "Output", 150, mOutputPanel, false); - SetupTab(outputTabbedView, "Immediate", 150, mImmediatePanel, false); + SetupTab(outputTabbedView, "Output", GS!(150), mOutputPanel, false); + SetupTab(outputTabbedView, "Immediate", GS!(150), mImmediatePanel, false); //outputTabbedView.AddTab("Find Results", 150, mFindResultsPanel, false); var watchTabbedView = CreateTabbedView(); - watchTabbedView.SetRequestedSize(250, 250); + watchTabbedView.SetRequestedSize(GS!(250), GS!(250)); mDockingFrame.AddDockedWidget(watchTabbedView, outputTabbedView, DockingFrame.WidgetAlign.Left); SetupTab(watchTabbedView, "Auto", 150, mAutoWatchPanel, false); @@ -9867,7 +9889,8 @@ namespace IDE if (!mRunningTestScript) { // User setting can affect automated testing, so use default settings - mSettings.Load(); + if (!mIsFirstRun) + mSettings.Load(); mSettings.Apply(); mIsFirstRun = !mSettings.mLoadedSettings; #if !CLI && BF_PLATFORM_WINDOWS @@ -9979,10 +10002,6 @@ namespace IDE { loadedWorkspaceUserData = true; } - else - { - CreateDefaultLayout(); - } WorkspaceLoaded(); @@ -10019,9 +10038,13 @@ namespace IDE if (dpi >= 120) { mSettings.mEditorSettings.mUIScale = 100 * Math.Min(dpi / 96.0f, 4.0f); + mSettings.Apply(); } } + if (!loadedWorkspaceUserData) + CreateDefaultLayout(); + UpdateTitle(); mMainWindow.SetMinimumSize(GS!(480), GS!(360)); mMainWindow.mIsMainWindow = true; @@ -11523,6 +11546,7 @@ namespace IDE { if (editData.mLoadedHash.GetKind() != .None) { + File.WriteAllText(@"c:\temp\test.txt", editData.mQueuedContent).IgnoreError(); editData.mLoadedHash = SourceHash.Create(editData.mLoadedHash.GetKind(), editData.mQueuedContent); } }) case .Err(let err))