-
Notifications
You must be signed in to change notification settings - Fork 1
Chore/fix env for windows #31
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
d940dd1
581a32d
7944ac2
343e933
d43ab04
1045245
7399e72
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,6 +2,8 @@ | |
| using UnityEngine; | ||
| using SocketIOClient; | ||
| using CandyCoded.env; | ||
| using System.IO; | ||
| using System.Collections.Generic; | ||
|
|
||
| public class PlayerMovement : MonoBehaviour | ||
| { | ||
|
|
@@ -28,6 +30,32 @@ public class PlayerMovement : MonoBehaviour | |
| public Vector2 maxCameraPosition; | ||
| private string socketUrl; | ||
|
|
||
| private void LoadEnvVariables() | ||
| { | ||
| string envFilePath = Path.Combine(Application.dataPath, ".env"); | ||
|
|
||
| if (File.Exists(envFilePath)) | ||
| { | ||
| foreach (var line in File.ReadAllLines(envFilePath)) | ||
| { | ||
| if (!string.IsNullOrWhiteSpace(line) && !line.StartsWith("#")) | ||
| { | ||
| var keyValue = line.Split('='); | ||
| if (keyValue.Length == 2) | ||
| { | ||
| var key = keyValue[0].Trim(); | ||
| var value = keyValue[1].Trim(); | ||
| Environment.SetEnvironmentVariable(key, value); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| else | ||
| { | ||
| Debug.LogError(".env file not found at " + envFilePath); | ||
| } | ||
| } | ||
|
|
||
| async void Start() | ||
| { | ||
| player = GetComponent<Player>(); | ||
|
|
@@ -53,6 +81,21 @@ async void Start() | |
| cameraWidth = cameraHeight * mainCamera.aspect; | ||
| } | ||
|
|
||
| try | ||
| { | ||
| LoadEnvVariables(); | ||
| var socketUrl = Environment.GetEnvironmentVariable("SOCKET_URL"); | ||
| var uri = new Uri(socketUrl); | ||
| clientSocket = SocketManager.Instance.ClientSocket; | ||
| if (clientSocket == null) | ||
| { | ||
| Debug.LogError("SocketIO client is null! Check SocketManager initialization."); | ||
| } | ||
| } | ||
| catch (Exception e) | ||
| { | ||
| Debug.LogError("Socket connection error: " + e.Message); | ||
| } | ||
|
Comment on lines
+84
to
+98
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Code dupliqué dans la méthode Start Il y a une duplication du bloc try-catch pour la configuration du socket. De plus, vous mélangez deux approches différentes pour charger les variables d'environnement. Suggestion de refactorisation : try
{
LoadEnvVariables();
- var socketUrl = Environment.GetEnvironmentVariable("SOCKET_URL");
- var uri = new Uri(socketUrl);
- clientSocket = SocketManager.Instance.ClientSocket;
- if (clientSocket == null)
- {
- Debug.LogError("SocketIO client is null! Check SocketManager initialization.");
- }
- }
- catch (Exception e)
- {
- Debug.LogError("Socket connection error: " + e.Message);
- }
- try
- {
env.TryParseEnvironmentVariable("SOCKET_URL", out string socketUrl);
var uri = new Uri(socketUrl);
clientSocket = SocketManager.Instance.ClientSocket;
if (clientSocket == null)
{
- Debug.LogError("SocketIO client is null! Check SocketManager initialization.");
+ Debug.LogError("Le client SocketIO est null ! Vérifiez l'initialisation du SocketManager.");
}
}
catch (Exception e)
{
- Debug.LogError("Socket connection error: " + e.Message);
+ Debug.LogError($"Erreur de connexion socket : {e.Message}");
}
|
||
| try | ||
| { | ||
| env.TryParseEnvironmentVariable("SOCKET_URL", out string socketUrl); | ||
|
|
@@ -165,10 +208,11 @@ async void SendData(string channel, string data) | |
| { | ||
| await clientSocket.EmitAsync(channel, data); | ||
| } | ||
|
|
||
| } | ||
| catch (Exception e) | ||
| { | ||
| Debug.LogError("Socket send error: " + e.Message); | ||
| UnityEngine.Debug.LogError("Socket send error: " + e.Message); | ||
| } | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -3,6 +3,8 @@ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| using UnityEngine; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| using SocketIOClient; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| using CandyCoded.env; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| using System.IO; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| using System.Collections.Generic; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| public class SocketManager : MonoBehaviour | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -25,8 +27,52 @@ private void Awake() | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private void LoadEnvVariables() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| string envFilePath = Path.Combine(Application.dataPath, ".env"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (File.Exists(envFilePath)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| foreach (var line in File.ReadAllLines(envFilePath)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (!string.IsNullOrWhiteSpace(line) && !line.StartsWith("#")) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| var keyValue = line.Split('='); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (keyValue.Length == 2) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| var key = keyValue[0].Trim(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| var value = keyValue[1].Trim(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Environment.SetEnvironmentVariable(key, value); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| else | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Debug.LogError(".env file not found at " + envFilePath); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+30
to
+54
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Améliorer la gestion des variables d'environnement La méthode
Voici une version améliorée : private void LoadEnvVariables()
{
- string envFilePath = Path.Combine(Application.dataPath, ".env");
+ string envFilePath = Path.Combine(Application.persistentDataPath, ".env");
if (File.Exists(envFilePath))
{
- foreach (var line in File.ReadAllLines(envFilePath))
+ try
{
- if (!string.IsNullOrWhiteSpace(line) && !line.StartsWith("#"))
+ foreach (var line in File.ReadAllLines(envFilePath))
{
- var keyValue = line.Split('=');
- if (keyValue.Length == 2)
+ if (!string.IsNullOrWhiteSpace(line) && !line.StartsWith("#"))
{
- var key = keyValue[0].Trim();
- var value = keyValue[1].Trim();
- Environment.SetEnvironmentVariable(key, value);
+ var keyValue = line.Split(new[] { '=' }, 2);
+ if (keyValue.Length == 2)
+ {
+ var key = keyValue[0].Trim();
+ var value = keyValue[1].Trim();
+ if (!string.IsNullOrEmpty(key) && !string.IsNullOrEmpty(value))
+ {
+ Environment.SetEnvironmentVariable(key, value);
+ }
+ }
}
}
}
+ catch (Exception e)
+ {
+ Debug.LogError($"Erreur lors de la lecture du fichier .env : {e.Message}");
+ }
}
else
{
- Debug.LogError(".env file not found at " + envFilePath);
+ Debug.LogError($"Fichier .env non trouvé : {envFilePath}");
}
}📝 Committable suggestion
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private async Task SetupSocket() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| LoadEnvVariables(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| try | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| var socketUrl = Environment.GetEnvironmentVariable("SOCKET_URL"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| var uri = new Uri(socketUrl); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ClientSocket = new SocketIOUnity(uri); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Connect to the server | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| await ClientSocket.ConnectAsync(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Debug.Log("Connected to backend via SocketManager."); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| catch (Exception e) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Debug.LogError("Socket connection error: " + e.Message); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+58
to
+74
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Code dupliqué dans SetupSocket Il y a une duplication du bloc try-catch pour la connexion socket. De plus, deux approches différentes sont utilisées pour récupérer l'URL du socket. Voici la correction proposée : private async Task SetupSocket()
{
LoadEnvVariables();
try
{
- var socketUrl = Environment.GetEnvironmentVariable("SOCKET_URL");
- var uri = new Uri(socketUrl);
- ClientSocket = new SocketIOUnity(uri);
-
- // Connect to the server
- await ClientSocket.ConnectAsync();
- Debug.Log("Connected to backend via SocketManager.");
- }
- catch (Exception e)
- {
- Debug.LogError("Socket connection error: " + e.Message);
- }
-
- try
- {
- env.TryParseEnvironmentVariable("SOCKET_URL", out string socketUrl);
+ if (!env.TryParseEnvironmentVariable("SOCKET_URL", out string socketUrl))
+ {
+ throw new Exception("Variable d'environnement SOCKET_URL non définie");
+ }
var uri = new Uri(socketUrl);
ClientSocket = new SocketIOUnity(uri);
// Connect to the server
await ClientSocket.ConnectAsync();
- Debug.Log("Connected to backend via SocketManager.");
+ Debug.Log("Connecté au backend via SocketManager.");
}
catch (Exception e)
{
- Debug.LogError("Socket connection error: " + e.Message);
+ Debug.LogError($"Erreur de connexion socket : {e.Message}");
}
}📝 Committable suggestion
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| try | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| env.TryParseEnvironmentVariable("SOCKET_URL", out string socketUrl); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Attention à la gestion des chemins de fichiers sur Windows
La méthode
LoadEnvVariablesprésente plusieurs points à améliorer :Path.Combineest bonne pour la compatibilité Windows, maisApplication.dataPathn'est peut-être pas le meilleur choix pour stocker le fichier.env.CandyCoded.envdéjà importé.Suggestions d'amélioration :
private void LoadEnvVariables() { - string envFilePath = Path.Combine(Application.dataPath, ".env"); + string envFilePath = Path.Combine(Application.persistentDataPath, ".env"); if (File.Exists(envFilePath)) { foreach (var line in File.ReadAllLines(envFilePath)) { if (!string.IsNullOrWhiteSpace(line) && !line.StartsWith("#")) { var keyValue = line.Split('='); if (keyValue.Length == 2) { var key = keyValue[0].Trim(); var value = keyValue[1].Trim(); + if (string.IsNullOrEmpty(key) || string.IsNullOrEmpty(value)) + { + Debug.LogWarning($"Variable d'environnement invalide : {line}"); + continue; + } Environment.SetEnvironmentVariable(key, value); } } } } else { - Debug.LogError(".env file not found at " + envFilePath); + Debug.LogError($"Fichier .env non trouvé : {envFilePath}"); } }📝 Committable suggestion