Skip to content
This repository has been archived by the owner on Nov 2, 2024. It is now read-only.

Commit

Permalink
Fixes Editor Dotnet Module Crash when performing ALC reload (DotNet b…
Browse files Browse the repository at this point in the history
…uild)
  • Loading branch information
Delsin-Yu committed Oct 9, 2023
1 parent bd6eb8a commit ff09c1f
Show file tree
Hide file tree
Showing 17 changed files with 349 additions and 333 deletions.
4 changes: 3 additions & 1 deletion GD-AtlasTexture-Creator.csproj
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
<Project Sdk="Godot.NET.Sdk/4.1.1">
<Project Sdk="Godot.NET.Sdk/4.1.2">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<EnableDynamicLoading>true</EnableDynamicLoading>
<RootNamespace>GDAtlasTextureCreator</RootNamespace>
</PropertyGroup>
<ItemGroup>
<Compile Remove="Build\**" />
<Compile Remove="ReleaseBuild\**" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Remove="Build\**" />
<EmbeddedResource Remove="ReleaseBuild\**" />
</ItemGroup>
</Project>
37 changes: 16 additions & 21 deletions addons/deyu_atlas_texture_creator_window/EditingAtlasTextureInfo.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
#if TOOLS

#region

using System.Collections.Generic;
using System.IO;
using System.Linq;
using Godot;

#endregion

namespace DEYU.GDUtilities.UnityAtlasTextureCreatorUtility;

public class EditingAtlasTextureInfo
Expand All @@ -35,6 +31,21 @@ private EditingAtlasTextureInfo(AtlasTexture backingAtlasTexture, Rect2 region,

public bool Modified { get; private set; }

private static class GdPath
{
public static string Combine(string pathA, string pathB)
{
var newPath = Path.Combine(pathA, pathB);
return ToGdPath(newPath);
}

public static string ToGdPath(string path)
{
var unixStyledPath = path.Replace("\\", "/");
return unixStyledPath.Insert(unixStyledPath.IndexOf('/'), "/");
}
}

public static EditingAtlasTextureInfo Create((AtlasTexture atlasTexture, string resourcePath) data)
{
var backingAtlasTexture = data.atlasTexture;
Expand Down Expand Up @@ -127,7 +138,7 @@ public string ApplyChanges(Texture2D sourceTexture, string sourceTextureDirector
{
Atlas = sourceTexture
};
m_ResourcePath = GDPath.Combine(sourceTextureDirectory, $"{Name}.tres");
m_ResourcePath = GdPath.Combine(sourceTextureDirectory, $"{Name}.tres");
}

m_BackingAtlasTexture.Region = Region;
Expand All @@ -150,21 +161,5 @@ public void DiscardChanges()
FilterClip = m_BackingAtlasTexture.FilterClip;
Modified = false;
}

private static class GDPath
{

public static string Combine(string pathA, string pathB)
{
var newPath = Path.Combine(pathA, pathB);
return ToGDPath(newPath);
}

public static string ToGDPath(string path)
{
var unixStyledPath = path.Replace("\\", "/");
return unixStyledPath.Insert(unixStyledPath.IndexOf('/'), "/");
}
}
}
#endif
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System;
using System.Diagnostics.CodeAnalysis;
using Godot;
using Range = Godot.Range;

namespace DEYU.GDUtilities.UnityAtlasTextureCreatorUtility;

public partial class UnityAtlasTextureCreator
{
private static void RegLineEdit([NotNull] LineEdit control, LineEdit.TextChangedEventHandler call) => control.TextChanged += call;

private static void RegRangeValueChanged([NotNull] Range control, Range.ValueChangedEventHandler call) => control.ValueChanged += call;

private static void RegButtonPressed([NotNull] BaseButton control, Action call) => control.Pressed += call;

private static void RegButtonToggled([NotNull] BaseButton control, BaseButton.ToggledEventHandler call) => control.Toggled += call;

private static void RegOptionButtonItemSelected([NotNull] OptionButton control, OptionButton.ItemSelectedEventHandler call) => control.ItemSelected += call;

private static void RegResourceChanged([NotNull] Resource resource, Action call) => resource.Changed += call;
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#if TOOLS


using System;
using System.IO;
using System.Text.RegularExpressions;
Expand All @@ -7,38 +9,32 @@ namespace DEYU.GDUtilities.UnityAtlasTextureCreatorUtility;

public partial class UnityAtlasTextureCreator
{
private static class GDPath
private static class GdPath
{
private static readonly Regex PrefixNameRegex = new(@"(?<captured>.+):[\/\\]*.*", RegexOptions.Compiled);
private static readonly Regex SuffixNameRegex = new(@".+:[\/\\]*(?<captured>.*)", RegexOptions.Compiled);
private static readonly Regex s_PrefixNameRegex = new(@"(?<captured>.+):[\/\\]*.*", RegexOptions.Compiled);
private static readonly Regex s_SuffixNameRegex = new(@".+:[\/\\]*(?<captured>.*)", RegexOptions.Compiled);

public static string GetDirectoryName(string path)
{
var directoryNameRaw = Path.GetDirectoryName(path);
return ToGDPath(directoryNameRaw);
return ToGdPath(directoryNameRaw);
}
public static string GetFileNameWithoutExtension(string path) =>

public static string GetFileNameWithoutExtension(string path) =>
Path.GetFileNameWithoutExtension(path);

public static string ToGDPath(string path)

public static string ToGdPath(string path)
{
var unixStyledPath = path.Replace("\\", "/");
var prefixMatch = PrefixNameRegex.Match(unixStyledPath);
if (!prefixMatch.Success)
{
throw new FormatException(path);
}

var suffixMatch = SuffixNameRegex.Match(unixStyledPath);
if (suffixMatch.Success)
{
return $"{prefixMatch.Groups["captured"]}://{suffixMatch.Groups["captured"]}";
}

var prefixMatch = s_PrefixNameRegex.Match(unixStyledPath);
if (!prefixMatch.Success) throw new FormatException(path);

var suffixMatch = s_SuffixNameRegex.Match(unixStyledPath);
if (suffixMatch.Success) return $"{prefixMatch.Groups["captured"]}://{suffixMatch.Groups["captured"]}";

return $"{prefixMatch.Groups["captured"]}://";
}
}
}
#endif
#endif
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
#if TOOLS

#region

using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using Godot;
using Godot.Collections;

#endregion

namespace DEYU.GDUtilities.UnityAtlasTextureCreatorUtility;

// This script contains the private fields and general api used by the UnityAtlasTextureCreator

public partial class UnityAtlasTextureCreator
Expand All @@ -19,30 +15,55 @@ public partial class UnityAtlasTextureCreator
private readonly Vector2[] m_HandlePositionBuffer = new Vector2[8];
private readonly Array m_OneLengthArray = new(new Variant[1]);
private string m_CurrentSourceTexturePath;
private Dragging m_DraggingHandle;
private Vector2 m_DraggingHandlePosition;
private Rect2 m_DraggingHandleStartRegion;
private Vector2 m_DraggingMousePositionOffset;
private Vector2 m_DrawOffsets;
private float m_DrawZoom;
private EditorFileSystem m_EditorFileSystem;

private EditorPlugin m_EditorPlugin;
private EditorUndoRedoManager m_EditorUndoRedoManager;

private EditingAtlasTextureInfo m_InspectingAtlasTextureInfo;
private Texture2D m_InspectingTex;
private string m_InspectingTexName;
private bool m_IsDragging;
private Rect2 m_ModifyingRegionBuffer;
private CanvasTexture m_PreviewTex;
private bool m_RequestCenter;
private bool m_UpdatingScroll;

// ReSharper disable once IdentifierTypo
private ViewPannerCSharpImpl m_ViewPanner;

private EditingAtlasTextureInfo m_InspectingAtlasTextureInfo;
private bool m_IsDragging;
private Dragging m_DraggingHandle;
private Rect2 m_ModifyingRegionBuffer;
private Rect2 m_DraggingHandleStartRegion;
private Vector2 m_DraggingHandlePosition;
private Vector2 m_DraggingMousePositionOffset;

[NotNull]
private ViewPannerCSharpImpl ViewPanner
{
get
{
if (m_ViewPanner is null)
{
var settings = m_EditorPlugin.GetEditorInterface().GetEditorSettings();

m_ViewPanner = new();
m_ViewPanner.SetCallbacks(Pan, ZoomOnPositionScroll);
m_ViewPanner.Setup(
settings.Get("editors/panning/sub_editors_panning_scheme").As<ViewPannerCSharpImpl.ControlScheme>(),
new(), // settings.GetShortcut("canvas_item_editor/pan_view"); // This api only exists in native side, Sad :(
settings.Get("editors/panning/simple_panning").As<bool>()
);
}

return m_ViewPanner;
}
}


/// <summary>
/// Update all UI controls based on the status of <see cref="m_InspectingTex"/>, <see cref="m_EditingAtlasTexture"/> and <see cref="m_InspectingAtlasTextureInfo"/>
/// Update all UI controls based on the status of <see cref="m_InspectingTex" />, <see cref="m_EditingAtlasTexture" />
/// and <see cref="m_InspectingAtlasTextureInfo" />
/// </summary>
private void UpdateControls()
{
Expand Down Expand Up @@ -76,7 +97,7 @@ private void UpdateControls()
}

/// <summary>
/// Method called when the inspected texture is changed
/// Method called when the inspected texture is changed
/// </summary>
private void OnTexChanged()
{
Expand All @@ -86,7 +107,7 @@ private void OnTexChanged()
}

/// <summary>
/// Method to update the inspection of the current texture
/// Method to update the inspection of the current texture
/// </summary>
private void UpdateInspectingTexture()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,15 @@
#if TOOLS

#region

using System;
using System.Collections.Generic;
using System.Linq;
using Godot;

#endregion

namespace DEYU.GDUtilities.UnityAtlasTextureCreatorUtility;

// This script contains the exports and api used by the Top Bar Section of the UnityAtlasTextureCreator

public partial class UnityAtlasTextureCreator
{
private enum SnapMode { NoneSnap, PixelSnap }

private enum ScanMode { SourceFolderOnly, WholeProject }

private SnapMode m_CurrentSnapMode = SnapMode.NoneSnap;

[Export, ExportSubgroup("Top Bar Section")] private OptionButton SnapModeButton { get; set; }
Expand All @@ -37,8 +28,12 @@ private SnapMode CurrentSnapMode
}
}

private enum SnapMode { NoneSnap, PixelSnap }

private enum ScanMode { SourceFolderOnly, WholeProject }

/// <summary>
/// Initialize the top bar section with editor settings
/// Initialize the top bar section with editor settings
/// </summary>
/// <param name="settings"></param>
private void InitializeTopBarSection(EditorSettings settings)
Expand All @@ -52,19 +47,15 @@ private void InitializeTopBarSection(EditorSettings settings)
)
.As<SnapMode>();

SnapModeButton.ItemSelected += p_mode => CurrentSnapMode = (SnapMode)p_mode;
SnapModeButton.Selected = (int)CurrentSnapMode;
RegOptionButtonItemSelected(SnapModeButton, pMode => CurrentSnapMode = (SnapMode)pMode);
RegButtonPressed(ScanAtlasInFolderButton, () => ScanAtlasTexture(ScanMode.SourceFolderOnly, m_EditingAtlasTexture));
RegButtonPressed(ScanAtlasInProjectButton, () => ScanAtlasTexture(ScanMode.WholeProject, m_EditingAtlasTexture));

ScanAtlasInFolderButton.Pressed +=
() =>
ScanAtlasTexture(ScanMode.SourceFolderOnly, m_EditingAtlasTexture);
ScanAtlasInProjectButton.Pressed +=
() =>
ScanAtlasTexture(ScanMode.WholeProject, m_EditingAtlasTexture);
SnapModeButton.Selected = (int)CurrentSnapMode;
}

/// <summary>
/// Scan and populate atlas textures based on the selected scan mode
/// Scan and populate atlas textures based on the selected scan mode
/// </summary>
private void ScanAtlasTexture(ScanMode scanMode, List<EditingAtlasTextureInfo> editingAtlasTextureInfoCache)
{
Expand All @@ -76,7 +67,7 @@ private void ScanAtlasTexture(ScanMode scanMode, List<EditingAtlasTextureInfo> e
{
case ScanMode.SourceFolderOnly:
var sourcePath = m_InspectingTex.ResourcePath;
var dirPath = GDPath.GetDirectoryName(sourcePath);
var dirPath = GdPath.GetDirectoryName(sourcePath);
EditorFileSystemDirectory directory;
directory = m_EditorFileSystem.GetFilesystemPath(dirPath);
FindMatchingSourceTextureInDirectory(m_InspectingTex, collection, directory);
Expand All @@ -95,7 +86,7 @@ private void ScanAtlasTexture(ScanMode scanMode, List<EditingAtlasTextureInfo> e
}

/// <summary>
/// Recursive method to find matching source textures in a directory and its subdirectories
/// Recursive method to find matching source textures in a directory and its subdirectories
/// </summary>
private static void FindMatchingSourceTextureInDirectoryRecursive(Texture2D sourceTexture, ICollection<(AtlasTexture, string)> matchedAtlasTexture, EditorFileSystemDirectory directory)
{
Expand All @@ -110,7 +101,8 @@ private static void FindMatchingSourceTextureInDirectoryRecursive(Texture2D sour
}

/// <summary>
/// Scans and acquire the <see cref="AtlasTexture"/> with the <see cref="AtlasTexture.Atlas"/> matching the provided <paramref name="sourceTexture"/> from the providing <paramref name="directory"/>
/// Scans and acquire the <see cref="AtlasTexture" /> with the <see cref="AtlasTexture.Atlas" /> matching the provided
/// <paramref name="sourceTexture" /> from the providing <paramref name="directory" />
/// </summary>
private static void FindMatchingSourceTextureInDirectory(Texture2D sourceTexture, ICollection<(AtlasTexture, string)> matchedAtlasTexture, EditorFileSystemDirectory directory)
{
Expand All @@ -125,4 +117,4 @@ private static void FindMatchingSourceTextureInDirectory(Texture2D sourceTexture
}
}

#endif
#endif
Loading

0 comments on commit ff09c1f

Please sign in to comment.