Skip to content
This repository was archived by the owner on Feb 3, 2026. It is now read-only.

Commit 439d5d0

Browse files
committed
Force relative paths in settings
1 parent 43983e2 commit 439d5d0

3 files changed

Lines changed: 45 additions & 26 deletions

File tree

Assets/GameScriptSettings.asset

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ MonoBehaviour:
1515
MaxFlags: 0
1616
InitialConversationPool: 1
1717
PreventSingleNodeChoices: 1
18-
DatabasePath: D:/Work/GameScriptUnity/Database/TestDB
19-
DatabaseVersion:
20-
GeneratedPath:
21-
GameDataPath: /Users/eric/Work/GameScriptUnity/Assets/StreamingAssets/GameScript
18+
DatabasePath: Database/TestDB
19+
DatabaseVersion:
20+
GeneratedPath:
21+
GameDataPath: GameScript

Packages/studio.shortsleeve.gamescriptunity/Editor/Menu/SettingsEditor.cs

Lines changed: 40 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,7 @@ public override VisualElement CreateInspectorGUI()
141141
return;
142142

143143
// Convert to relative and save
144-
string projectRoot = Path.GetFullPath(Path.Combine(Application.dataPath, ".."));
145-
string relativeDbPath = GetRelativePath(projectRoot, absoluteDbPath);
144+
string relativeDbPath = GetRelativePathFromProjectRoot(absoluteDbPath);
146145
serializedObject.FindProperty("DatabasePath").stringValue = relativeDbPath;
147146
serializedObject.ApplyModifiedProperties();
148147

@@ -201,6 +200,15 @@ public override VisualElement CreateInspectorGUI()
201200
{
202201
// Convert to relative and save
203202
string relativePath = GetRelativePathFromStreamingAssets(absolutePath);
203+
if (relativePath == null)
204+
{
205+
EditorUtility.DisplayDialog(
206+
"Invalid Path",
207+
"The selected folder must be within the StreamingAssets folder.",
208+
"OK"
209+
);
210+
return;
211+
}
204212
serializedObject.FindProperty("GameDataPath").stringValue = relativePath;
205213
serializedObject.ApplyModifiedProperties();
206214
// Update display with absolute path
@@ -220,13 +228,22 @@ public override VisualElement CreateInspectorGUI()
220228
{
221229
string absolutePath = EditorUtility.OpenFolderPanel(
222230
$"Select {RuntimeConstants.k_AppName} Folder",
223-
"",
231+
Application.dataPath,
224232
""
225233
);
226234
if (!string.IsNullOrEmpty(absolutePath))
227235
{
228236
// Convert to relative and save
229237
string relativePath = GetRelativePathFromAssets(absolutePath);
238+
if (relativePath == null)
239+
{
240+
EditorUtility.DisplayDialog(
241+
"Invalid Path",
242+
"The selected folder must be within the Assets folder.",
243+
"OK"
244+
);
245+
return;
246+
}
230247
serializedObject.FindProperty("GeneratedPath").stringValue = relativePath;
231248
serializedObject.ApplyModifiedProperties();
232249
// Update display with absolute path
@@ -528,36 +545,38 @@ private bool IsValidSubFolder(string path, string parent)
528545
return false;
529546
}
530547

531-
private string GetRelativePath(string fromPath, string toPath)
548+
/// <summary>
549+
/// Gets a relative path that must be within the specified parent folder.
550+
/// Returns null if the path is outside the parent folder.
551+
/// </summary>
552+
private string GetRelativeSubPath(string parentPath, string absolutePath)
532553
{
533-
if (string.IsNullOrEmpty(fromPath) || string.IsNullOrEmpty(toPath))
534-
return toPath;
554+
if (string.IsNullOrEmpty(parentPath) || string.IsNullOrEmpty(absolutePath))
555+
return null;
535556

536-
fromPath = Path.GetFullPath(fromPath).Replace('\\', '/');
537-
toPath = Path.GetFullPath(toPath).Replace('\\', '/');
557+
string relativePath = Path.GetRelativePath(parentPath, absolutePath).Replace('\\', '/');
538558

539-
// Ensure paths have trailing separators for directory comparison
540-
if (!fromPath.EndsWith("/"))
541-
fromPath += "/";
542-
543-
// Check if toPath starts with fromPath
544-
if (toPath.StartsWith(fromPath, StringComparison.OrdinalIgnoreCase))
545-
{
546-
return toPath.Substring(fromPath.Length);
547-
}
559+
// If the relative path starts with "..", it's outside the parent folder
560+
if (relativePath.StartsWith(".."))
561+
return null;
548562

549-
// If not a subpath, return the absolute path
550-
return toPath;
563+
return relativePath;
551564
}
552565

553566
private string GetRelativePathFromStreamingAssets(string absolutePath)
554567
{
555-
return GetRelativePath(Application.streamingAssetsPath, absolutePath);
568+
return GetRelativeSubPath(Application.streamingAssetsPath, absolutePath);
556569
}
557570

558571
private string GetRelativePathFromAssets(string absolutePath)
559572
{
560-
return GetRelativePath(Application.dataPath, absolutePath);
573+
return GetRelativeSubPath(Application.dataPath, absolutePath);
574+
}
575+
576+
private string GetRelativePathFromProjectRoot(string absolutePath)
577+
{
578+
string projectRoot = Path.GetFullPath(Path.Combine(Application.dataPath, ".."));
579+
return Path.GetRelativePath(projectRoot, absolutePath).Replace('\\', '/');
561580
}
562581
#endregion
563582
}

Packages/studio.shortsleeve.gamescriptunity/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "studio.shortsleeve.gamescriptunity",
33
"displayName": "GameScriptUnity",
4-
"version": "0.0.22",
4+
"version": "0.0.23",
55
"unity": "2023.2",
66
"description": "Cross-platform dialogue middleware",
77
"keywords": [

0 commit comments

Comments
 (0)