Skip to content

Commit

Permalink
Make logger/Image generator always use current save path
Browse files Browse the repository at this point in the history
  • Loading branch information
Piotrekol committed May 23, 2016
1 parent f763bbb commit 9f64a66
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 22 deletions.
43 changes: 35 additions & 8 deletions osu!StreamCompanion/Code/Core/Loggers/FileLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,55 @@ public class FileLogger : ILogger
private ISaver _saver;
private readonly Settings _settings;
DateTime startTime = DateTime.Today;
private readonly string _relativeSaveLocation = @"Logs/";
private string CurrentLogSaveLocation = "";


private readonly string _logsSaveFolderName = @"Logs\";

public FileLogger(ISaver saver, Settings settings)
{
_saver = saver;
_settings = settings;
CreateDirectory(_saver.SaveDirectory);
CurrentLogSaveLocation = GetRelativeSaveLocation();

CreateLogsDirectory();
}

private void CreateDirectory(string baseFolder)
private void CreateLogsDirectory()
{
string dir = Path.Combine(baseFolder, _relativeSaveLocation);
string dir = Path.Combine(_saver.SaveDirectory, _logsSaveFolderName);

if (!Directory.Exists(dir))
{
Directory.CreateDirectory(dir);
}

CurrentLogSaveLocation = GetRelativeSaveLocation();
}


private bool SaveDirectoryExists()
{
return Directory.Exists(Path.Combine(_saver.SaveDirectory, _logsSaveFolderName));
}

private string GetRelativeSaveLocation()
{
return Path.Combine(_logsSaveFolderName, startTime.ToString("yyyy-MM-dd") + ".txt");
}
public void Log(string logMessage, LogLevel loglvevel, params string[] vals)
{
if (_settings.Get("LogLevel", LogLevel.Disabled.GetHashCode()) >= loglvevel.GetHashCode())
_saver.append(_relativeSaveLocation + startTime.ToString("yyyy-MM-dd") + ".txt", string.Format(logMessage, vals) + Environment.NewLine);
try
{
if (_settings.Get("LogLevel", LogLevel.Disabled.GetHashCode()) >= loglvevel.GetHashCode())
_saver.append(CurrentLogSaveLocation, string.Format(logMessage, vals) + Environment.NewLine);
}
catch
{
if (SaveDirectoryExists())
throw;

CreateLogsDirectory();
Log(logMessage, loglvevel, vals);
}
}

public void SetSaveHandle(ISaver saver)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@
using System.Drawing.Imaging;
using System.IO;
using osu_StreamCompanion.Code.Core;
using osu_StreamCompanion.Code.Interfeaces;
using Point = System.Drawing.Point;

namespace osu_StreamCompanion.Code.Modules.ModImageGenerator.API
{
public class ImageGenerator
public class ImageGenerator:ISaveRequester
{
private Settings _settings;
public string ImageDirectory { get; set; }
public string ImageFullPath { get; set; }
public ImageGenerator(Settings settings, string imageDirectory)
private ISaver _saver;
public string ImagesFolderName { get; set; }
public ImageGenerator(Settings settings, string imagesFolderName)
{
ImageDirectory = imageDirectory;
ImageFullPath = Path.Combine(ImageDirectory, "modImage.png");
ImagesFolderName = imagesFolderName;
_settings = settings;
}

Expand All @@ -37,7 +37,7 @@ public Bitmap GenerateImage(string[] modsList)

foreach (var mod in modsList)
{
string effectiveModPath = Path.Combine(ImageDirectory, mod.ToUpper() + ".png");
string effectiveModPath = Path.Combine(_saver.SaveDirectory, ImagesFolderName, mod.ToUpper() + ".png");
if (ModImageExists(effectiveModPath))
{
validModPaths.Add(effectiveModPath);
Expand Down Expand Up @@ -133,6 +133,11 @@ public Image SetImageOpacity(Image image, float opacity)

private enum DrawSide { Left = 0, Right = 1 }
private enum DrawDirection { FromLeftToRight = 0, FromRightToLeft = 1 }

public void SetSaveHandle(ISaver saver)
{
_saver = saver;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,16 @@ class ModImageGenerator : IModule, IMapDataReplacements, ISettingsProvider, ISav
public bool Started { get; set; }
ImageDeployer _imageDeployer;
ImageGenerator _imageGenerator;
private string _imagesDirectory;
private string _FullPathOfCreatedImage;
private ModImageGeneratorSettings _modImageGeneratorSettings;

public void Start(ILogger logger)
{
_imagesDirectory = Path.Combine(_saver.SaveDirectory, @"Images" + Path.DirectorySeparatorChar);
_FullPathOfCreatedImage = Path.Combine(_saver.SaveDirectory, "ModImage.png");
_imageDeployer = new ImageDeployer(_imagesDirectory);
var imagesFolderName = "Images";
_imageDeployer = new ImageDeployer(Path.Combine(_saver.SaveDirectory,imagesFolderName));
_imageDeployer.DeployImages();
_imageDeployer.CreateReadMe();
_imageGenerator = new ImageGenerator(_settings, _imagesDirectory);
_imageGenerator = new ImageGenerator(_settings, @"Images");
_imageGenerator.SetSaveHandle(_saver);
}

public Dictionary<string, string> GetMapReplacements(MapSearchResult map)
Expand All @@ -40,9 +38,10 @@ public Dictionary<string, string> GetMapReplacements(MapSearchResult map)
{
if (!string.IsNullOrWhiteSpace(map.Mods))
{
var fullPathOfCreatedImage = Path.Combine(_saver.SaveDirectory, "ModImage.png");
using (Bitmap img = _imageGenerator.GenerateImage(map.Mods.Split(',')))
{
img.Save(_FullPathOfCreatedImage, ImageFormat.Png);
img.Save(fullPathOfCreatedImage, ImageFormat.Png);
}
}
}
Expand Down

0 comments on commit 9f64a66

Please sign in to comment.