Skip to content

Commit ac7ea3e

Browse files
[src] Add SofaUtils to add static method to get paths depending on the OS (#257)
* [src] Add SofaUtils to add static method to get paths depending on the OS * [src] Update all code to use new SofaUtils method * Update gitignore * small editor modification * backup work * Fix force build path in the postProcessing script as it is for the build but still run in the Editor mode * small line added for carving plugin --------- Co-authored-by: PierreFonda3D <pierre.fonda@infinytech3d.com>
1 parent 00da391 commit ac7ea3e

7 files changed

Lines changed: 148 additions & 55 deletions

File tree

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ License/*
77
Core/Scripts/Editor/Tests/logs/
88
Tests/logs/
99
Core/Scripts/UI/DataManager/DynamicDataSaves/*.JSON
10-
Core/Scripts/UI/DataManager/DynamicDataSaves/*.meta
10+
Core/Scripts/UI/DataManager/DynamicDataSaves/*.meta
11+
Core/Plugins/Native/

Core/Plugins/Native/x64.meta

Lines changed: 0 additions & 9 deletions
This file was deleted.

Core/Plugins/SofaUnityAPI/SofaContextAPI.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,10 @@ public SofaContextAPI(bool async)
101101
RegenerateSofaIni();
102102
#endif
103103
// load the sofaIni file
104-
string pathIni = Application.dataPath + "/SofaUnity/Core/Plugins/Native/x64/sofa.ini";
105-
string sharePath = sofaPhysicsAPI_loadSofaIni(m_native, pathIni);
104+
string iniPath = SofaUtils.GetPluginFullPrefixPath() + "sofa.ini";
105+
Debug.Log("[SofaUnity] loading sofa.ini at: " + iniPath);
106+
107+
string sharePath = sofaPhysicsAPI_loadSofaIni(m_native, iniPath);
106108

107109
if (sharePath.Contains("Error"))
108110
{
@@ -139,7 +141,7 @@ private static void RegenerateSofaIni()
139141
string pluginsPath = dataPath + "/Plugins/x86_64";
140142
string scenesPath = dataPath + "/SofaUnity/scenes/SofaScenes";
141143
string licensePath = dataPath + "/License/";
142-
string iniPath = dataPath + "/SofaUnity/Core/Plugins/Native/x64/sofa.ini";
144+
string iniPath = dataPath + SofaUtils.GetNativePluginPath() + "sofa.ini";
143145

144146
using (StreamWriter iniFile = new StreamWriter(iniPath))
145147
{
@@ -285,6 +287,7 @@ public void loadPlugin(string pluginName)
285287
{
286288
if (m_isReady)
287289
{
290+
Debug.Log("SofaContextAPI.loadPlugin using fullPluginPath: " + pluginName);
288291
int res = sofaPhysicsAPI_loadPlugin(m_native, pluginName);
289292
if (res < 0)
290293
Debug.LogError("SofaContextAPI::loadPlugin: " + pluginName + ", method returns: " + SofaDefines.msg_error[res]);
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using Unity.VisualScripting;
4+
using UnityEngine;
5+
6+
namespace SofaUnityAPI
7+
{
8+
static public class SofaUtils
9+
{
10+
/// Application.dataPath: Unity Editor: <path to project folder>/Assets
11+
static public string GetPluginFullPrefixPath()
12+
{
13+
string pluginPath;
14+
#if UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN
15+
if (Application.isEditor)
16+
pluginPath = "/SofaUnity/Core/Plugins/Native/Windows/x64/";
17+
else
18+
pluginPath = "/Plugins/x86_64/";
19+
#elif UNITY_EDITOR_OSX || UNITY_STANDALONE_OSX
20+
if (Application.isEditor)
21+
pluginPath = "/SofaUnity/Core/Plugins/Native/macOS/";
22+
else
23+
pluginPath = "/Plugins/x86_64/";
24+
#elif UNITY_EDITOR_LINUX || UNITY_STANDALONE_LINUX
25+
if (Application.isEditor)
26+
pluginPath = "/SofaUnity/Core/Plugins/Native/Linux/x86_64/";
27+
else
28+
pluginPath = "/Plugins/x86_64/";
29+
#elif UNITY_ANDROID
30+
return Application.persistentDataPath;
31+
#else
32+
throw new PlatformNotSupportedException();
33+
#endif
34+
return Application.dataPath + pluginPath;
35+
}
36+
37+
38+
static public string GetNativePluginPath()
39+
{
40+
string path;
41+
#if UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN
42+
if (Application.isEditor)
43+
path = "/SofaUnity/Core/Plugins/Native/Windows/x64/";
44+
else
45+
path = "/Plugins/x86_64/";
46+
#elif UNITY_EDITOR_OSX || UNITY_STANDALONE_OSX
47+
path = "/SofaUnity/Core/Plugins/Native/macOS/";
48+
#elif UNITY_EDITOR_LINUX || UNITY_STANDALONE_LINUX
49+
path = "/SofaUnity/Core/Plugins/Native/Linux/x86_64/";
50+
#endif
51+
return path;
52+
}
53+
54+
static public string GetNativeBuildPath()
55+
{
56+
string path;
57+
#if UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN
58+
path = "/Plugins/x86_64/";
59+
#elif UNITY_EDITOR_OSX || UNITY_STANDALONE_OSX
60+
path = "/SofaUnity/Core/Plugins/Native/macOS/";
61+
#elif UNITY_EDITOR_LINUX || UNITY_STANDALONE_LINUX
62+
path = "/SofaUnity/Core/Plugins/Native/Linux/x86_64/";
63+
#endif
64+
return path;
65+
}
66+
67+
68+
static public string GetPluginFullName(string pluginName)
69+
{
70+
string pluginFullPath = "";
71+
#if UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN
72+
pluginFullPath = pluginName + ".dll";
73+
#elif UNITY_EDITOR_LINUX || UNITY_STANDALONE_LINUX || UNITY_ANDROID
74+
pluginFullPath = "lib" + pluginName + ".so";
75+
#elif UNITY_EDITOR_OSX || UNITY_STANDALONE_OSX
76+
pluginFullPath = "lib" + pluginName + ".dylib";
77+
#endif
78+
return pluginFullPath;
79+
}
80+
81+
}
82+
}
83+

Core/Plugins/SofaUnityAPI/SofaUtils.cs.meta

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Core/Scripts/Core/System/PluginManager.cs

Lines changed: 32 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@
44
using UnityEditor;
55
using SofaUnityAPI;
66
using System.IO;
7+
using System;
8+
using Unity.VisualScripting;
9+
#if UNITY_EDITOR
10+
using UnityEditor.VersionControl;
11+
#endif
12+
713

814

915
namespace SofaUnity
@@ -63,6 +69,7 @@ public List<PluginInfo> GetPluginList()
6369
return m_availablePlugins;
6470
}
6571

72+
6673
public PluginInfo GetPluginByName(string pluginName)
6774
{
6875
for (int id = 0; id < m_availablePlugins.Count; id++)
@@ -81,8 +88,26 @@ public bool CheckPluginExists(string pluginName)
8188
{
8289
if (m_dllList == null)
8390
{
84-
string dllDirPath = Application.dataPath + "/SofaUnity/Core/Plugins/Native/x64/";
91+
92+
string dllDirPath = SofaUtils.GetPluginFullPrefixPath();// Application.dataPath + "/SofaUnity/Core/Plugins/Native/x64/";
93+
94+
#if UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN
8595
m_dllList = Directory.GetFiles(dllDirPath, "*.dll");
96+
#elif UNITY_EDITOR_OSX || UNITY_STANDALONE_OSX
97+
m_dllList = Directory.GetFiles(dllDirPath, "*.dylib");
98+
#elif UNITY_EDITOR_LINUX || UNITY_STANDALONE_LINUX
99+
m_dllList = Directory.GetFiles(dllDirPath, "*.so");
100+
#elif UNITY_ANDROID
101+
m_dllList = Directory.GetFiles(dllDirPath, "*.so");
102+
#else
103+
m_dllList = null;
104+
#endif
105+
106+
if (m_dllList == null)
107+
{
108+
Debug.LogError("PluginManager: Can't find any plugin in directory " + dllDirPath);
109+
return false;
110+
}
86111

87112
for (int i = 0; i < m_dllList.Length; i++)
88113
{
@@ -203,45 +228,21 @@ public void ClearSavedPlugin()
203228
}
204229

205230

206-
public string getPluginFullPrefixPath()
207-
{
208-
string pluginPath;
209-
if (Application.isEditor)
210-
pluginPath = "/SofaUnity/Core/Plugins/Native/x64/";
211-
else
212-
#if UNITY_ANDROID
213-
pluginPath = "/Plugins/Android/";
214-
#else
215-
pluginPath = "/Plugins/x86_64/";
216-
#endif
217-
return Application.dataPath + pluginPath;
218-
}
219-
220-
public string getPluginFullName(string pluginName)
221-
{
222-
string pluginFullPath = "";
223-
#if UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN
224-
pluginFullPath = pluginName + ".dll";
225-
#elif UNITY_EDITOR_LINUX || UNITY_STANDALONE_LINUX || UNITY_ANDROID
226-
pluginFullPath = "lib" + pluginName + ".so";
227-
#endif
228-
return pluginFullPath;
229-
}
230-
231231
/// Method to load the plugins one by one from the list of enable plugins
232232
public void LoadPlugins()
233233
{
234-
string fullPrefixPath = getPluginFullPrefixPath();
234+
string fullPrefixPath = SofaUtils.GetPluginFullPrefixPath();
235235

236236
// Internally load all default plugins from core and module
237+
Debug.Log("m_sofaAPI.loadDefaultPlugins using path: " + fullPrefixPath);
237238
m_sofaAPI.loadDefaultPlugins(fullPrefixPath);
238239

239240
foreach (PluginInfo plugin in m_savedPlugins)
240241
{
241242
if (!plugin.IsEnable)
242243
continue;
243244

244-
string fullPluginPath = getPluginFullPrefixPath() + getPluginFullName(plugin.Name);
245+
string fullPluginPath = SofaUtils.GetPluginFullPrefixPath() + SofaUtils.GetPluginFullName(plugin.Name);
245246
#if UNITY_EDITOR
246247
PluginInfo plug = GetPluginByName(plugin.Name);
247248
if (plug == null || plug.IsAvailable == false)
@@ -251,7 +252,7 @@ public void LoadPlugins()
251252
continue;
252253
}
253254
else
254-
{
255+
{
255256
m_sofaAPI.loadPlugin(fullPluginPath);
256257
}
257258
#else
@@ -262,7 +263,7 @@ public void LoadPlugins()
262263

263264
public void LoadPlugin(string pluginName)
264265
{
265-
string fullPluginPath = getPluginFullPrefixPath() + getPluginFullName(pluginName);
266+
string fullPluginPath = SofaUtils.GetPluginFullPrefixPath() + SofaUtils.GetPluginFullName(pluginName);
266267

267268
m_sofaAPI.loadPlugin(fullPluginPath);
268269
}
@@ -332,6 +333,7 @@ static void RegisterDefaultPlugin()
332333
PluginManager.Instance.AddPlugin("SofaPython3");
333334
PluginManager.Instance.AddPlugin("Tearing");
334335
PluginManager.Instance.AddPlugin("MultiThreading");
336+
PluginManager.Instance.AddPlugin("EnclosedCollisionPlugin");
335337
}
336338
#endif
337339
}

Core/Scripts/Editor/Config/CopyConfigPostProcessor.cs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using UnityEditor.Callbacks;
55
using UnityEngine.SceneManagement;
66
using System.Collections.Generic;
7+
using SofaUnityAPI;
78

89
namespace SofaUnity
910
{
@@ -15,15 +16,14 @@ public class CopyConfigPostProcessor
1516
/// </summary>
1617
static CopyConfigPostProcessor()
1718
{
18-
string sofaIniFile = Application.dataPath + "/SofaUnity/Core/Plugins/Native/x64/sofa.ini";
19+
string sofaIniFile = SofaUtils.GetPluginFullPrefixPath() + "sofa.ini";
1920
using (StreamWriter outputIniFile = new StreamWriter(sofaIniFile))
2021
{
2122
string SofaUnityDir = Application.dataPath + "/SofaUnity/scenes/SofaScenes";
2223
outputIniFile.WriteLine("SHARE_DIR=" + SofaUnityDir);
23-
outputIniFile.WriteLine("SHARE_DIR=C:/projects/sofa-src/share/");
2424
outputIniFile.WriteLine("EXAMPLES_DIR=" + SofaUnityDir);
2525
outputIniFile.WriteLine("LICENSE_DIR=" + Application.dataPath + "/SofaUnity/License/");
26-
outputIniFile.WriteLine("BUILD_DIR=" + Application.dataPath + "/SofaUnity/Core/Plugins/Native/x64/");
26+
outputIniFile.WriteLine("BUILD_DIR=" + SofaUtils.GetPluginFullPrefixPath());
2727
}
2828
}
2929

@@ -146,25 +146,26 @@ static void BuildForWindows(string pathToBuiltProject)
146146
string rootBuildPath = pathToBuiltProject.Replace(".exe", "") + "_Data";
147147

148148
// Create bin build path
149-
string binBuildPath = rootBuildPath + "/SofaUnity/Core/Plugins/Native/x64/";
149+
string binBuildPath = rootBuildPath + SofaUtils.GetNativeBuildPath();
150150
string dataBuildPath = rootBuildPath + "/SofaUnity/scenes/SofaScenes";
151+
string licenseBuildPath = rootBuildPath + "/License/";
151152

152153
Directory.CreateDirectory(binBuildPath);
153-
Debug.Log("[SofaUnity - BuildForWindows] Create directory: " + binBuildPath);
154+
Debug.Log("[SofaUnity - BuildForWindows] Create 'binBuildPath' directory: " + binBuildPath);
154155
Directory.CreateDirectory(dataBuildPath);
155-
Debug.Log("[SofaUnity - BuildForWindows] Create directory: " + dataBuildPath);
156+
Debug.Log("[SofaUnity - BuildForWindows] Create 'dataBuildPath' directory: " + dataBuildPath);
156157

157158
// Copy current License
158-
DirectoryCopy(Application.dataPath + "/SofaUnity/License/", rootBuildPath + "/License/", true);
159+
DirectoryCopy(Application.dataPath + "/SofaUnity/License/", licenseBuildPath, true);
159160

160161
// Update SOFA ini file with build dir paths
161162
string outputIniPath = Path.Combine(binBuildPath, "sofa.ini");
162163
using (StreamWriter outputIniFile = new StreamWriter(outputIniPath))
163164
{
164165
outputIniFile.WriteLine("SHARE_DIR=" + dataBuildPath);
165166
outputIniFile.WriteLine("EXAMPLES_DIR=" + dataBuildPath);
166-
outputIniFile.WriteLine("LICENSE_DIR=" + rootBuildPath + "/License/");
167-
outputIniFile.WriteLine("BUILD_DIR=" + rootBuildPath + "/Plugins/x86_64/");
167+
outputIniFile.WriteLine("LICENSE_DIR=" + licenseBuildPath);
168+
outputIniFile.WriteLine("BUILD_DIR=" + binBuildPath);
168169
Debug.Log("[SofaUnity - BuildForWindows] Generate: " + outputIniPath + " file.");
169170
}
170171

@@ -188,13 +189,13 @@ static void BuildForWindows(string pathToBuiltProject)
188189
}
189190

190191
// Copy python3 folder if exists for python scene build
191-
string sourcePython3Path = Application.dataPath + "/SofaUnity/Core/Plugins/Native/x64/python3/";
192-
string destPython3Path = rootBuildPath + "/Plugins/x86_64/python3/";
192+
string sourcePython3Path = SofaUtils.GetPluginFullPrefixPath() + "python3/";
193+
string destPython3Path = binBuildPath + "/python3/";
193194

194195
if (Directory.Exists(sourcePython3Path))
195196
{
196197
DirectoryCopy(sourcePython3Path, destPython3Path, true);
197-
Debug.Log("[SofaUnity - BuildForWindows] Copied python3 folder to: " + destPython3Path);
198+
Debug.Log("[SofaUnity - BuildForWindows] Copied python3 from directory: " + sourcePython3Path + " -----> " + destPython3Path);
198199
}
199200
else
200201
{

0 commit comments

Comments
 (0)