From 6f5834db8976cfc0ef1450d301c6c2f65b0ce5c1 Mon Sep 17 00:00:00 2001 From: Flamefire Date: Wed, 17 Apr 2013 14:50:35 +0200 Subject: [PATCH] Fixed .Net 4 and XP support --- Vocaluxe/Base/CLog.cs | 17 ++- Vocaluxe/CSplashScreen.cs | 66 +++++++++++ Vocaluxe/Lib/Draw/CDirect3D.cs | 66 +++++------ Vocaluxe/Program.cs | 179 ++++++++++++++--------------- Vocaluxe/Screens/CScreenCredits.cs | 4 +- Vocaluxe/Vocaluxe.csproj | 19 +-- 6 files changed, 200 insertions(+), 151 deletions(-) create mode 100644 Vocaluxe/CSplashScreen.cs diff --git a/Vocaluxe/Base/CLog.cs b/Vocaluxe/Base/CLog.cs index 610ebacf..c72f6a86 100644 --- a/Vocaluxe/Base/CLog.cs +++ b/Vocaluxe/Base/CLog.cs @@ -19,15 +19,14 @@ namespace Vocaluxe.Base public void Close() { - if (_LogFile != null) + if (_LogFile == null) + return; + try { - try - { - _LogFile.Flush(); - _LogFile.Close(); - } - catch (Exception) {} + _LogFile.Flush(); + _LogFile.Close(); } + catch (Exception) {} } public void Add(string text) @@ -47,7 +46,7 @@ namespace Vocaluxe.Base { _LogFile = new StreamWriter(Path.Combine(Environment.CurrentDirectory, _LogFileName), false, Encoding.UTF8); - _LogFile.WriteLine(_LogName + " " + CSettings.GetFullVersionText() + " " + DateTime.Now.ToString()); + _LogFile.WriteLine(_LogName + " " + CSettings.GetFullVersionText() + " " + DateTime.Now); _LogFile.WriteLine("----------------------------------------"); _LogFile.WriteLine(); } @@ -99,7 +98,7 @@ namespace Vocaluxe.Base { _NumErrors++; - _ErrorLog.Add(_NumErrors.ToString() + ") " + errorText); + _ErrorLog.Add(_NumErrors + ") " + errorText); _ErrorLog.Add(String.Empty); } #endregion LogError diff --git a/Vocaluxe/CSplashScreen.cs b/Vocaluxe/CSplashScreen.cs new file mode 100644 index 00000000..e6206794 --- /dev/null +++ b/Vocaluxe/CSplashScreen.cs @@ -0,0 +1,66 @@ +using System; +using System.Drawing; +using System.IO; +using System.Windows.Forms; +using Vocaluxe.Base; + +namespace Vocaluxe +{ + class CSplashScreen : Form + { + private readonly Bitmap _Logo; + + public CSplashScreen() + { + string path = Path.Combine(Environment.CurrentDirectory, Path.Combine(CSettings.FolderGraphics, CSettings.Logo)); + if (File.Exists(path)) + { + try + { + _Logo = new Bitmap(path); + ClientSize = new Size(_Logo.Width, _Logo.Height); + } + catch (Exception e) + { + CLog.LogError("Error loading logo: " + e.Message); + } + } + else + CLog.LogError("Can't find " + path); + + path = Path.Combine(Environment.CurrentDirectory, CSettings.Icon); + if (File.Exists(path)) + { + try + { + Icon = new Icon(path); + } + catch (Exception e) + { + CLog.LogError("Error loading icon: " + e.Message); + } + } + else + CLog.LogError("Can't find " + path); + + SetStyle(ControlStyles.SupportsTransparentBackColor, true); + BackColor = Color.Transparent; + + FormBorderStyle = FormBorderStyle.None; + Text = CSettings.ProgramName; + CenterToScreen(); + Show(); + } + + protected override void OnPaint(PaintEventArgs e) {} + + protected override void OnPaintBackground(PaintEventArgs e) + { + if (_Logo == null) + return; + + Graphics g = e.Graphics; + g.DrawImage(_Logo, new Rectangle(0, 0, Width, Height)); + } + } +} \ No newline at end of file diff --git a/Vocaluxe/Lib/Draw/CDirect3D.cs b/Vocaluxe/Lib/Draw/CDirect3D.cs index 631abfd7..63672c7f 100644 --- a/Vocaluxe/Lib/Draw/CDirect3D.cs +++ b/Vocaluxe/Lib/Draw/CDirect3D.cs @@ -1,4 +1,23 @@ -using System.Drawing.Drawing2D; +#region license +// /* +// This file is part of Vocaluxe. +// +// Vocaluxe is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// Vocaluxe is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with Vocaluxe. If not, see . +// */ +#endregion + +using System.Drawing.Drawing2D; using System.Threading; using SlimDX; using SlimDX.Direct3D9; @@ -117,6 +136,7 @@ namespace Vocaluxe.Lib.Draw _PresentParameters.MultisampleQuality = 0; //Apply antialiasing and check if antialiasing mode is supported + #region Antialiasing int quality = 1; MultisampleType msType; @@ -912,38 +932,11 @@ namespace Vocaluxe.Lib.Draw h = maxSize; } - //Older graphics card can only handle textures with sizes being powers of two - w = (int)_CheckForNextPowerOf2(w); - h = (int)_CheckForNextPowerOf2(h); - - Bitmap bmp2 = null; - byte[] data; - try - { - if (w != bmp.Width || h != bmp.Height) - { - //Create a new Bitmap with the new sizes - bmp2 = new Bitmap(w, h); - //Scale the texture - Graphics g = Graphics.FromImage(bmp2); - g.InterpolationMode = InterpolationMode.HighQualityBicubic; - g.SmoothingMode = SmoothingMode.HighQuality; - g.DrawImage(bmp, new Rectangle(0, 0, bmp2.Width, bmp2.Height)); - g.Dispose(); - bmp = bmp2; - } - - //Fill the new Bitmap with the texture data - BitmapData bmpData = bmp.LockBits(new Rectangle(0, 0, w, h), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb); - data = new byte[4 * w * h]; - Marshal.Copy(bmpData.Scan0, data, 0, data.Length); - bmp.UnlockBits(bmpData); - } - finally - { - if (bmp2 != null) - bmp2.Dispose(); - } + //Fill the new Bitmap with the texture data + BitmapData bmpData = bmp.LockBits(new Rectangle(0, 0, w, h), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb); + byte[] data = new byte[4 * w * h]; + Marshal.Copy(bmpData.Scan0, data, 0, data.Length); + bmp.UnlockBits(bmpData); return AddTexture(w, h, ref data); } @@ -997,10 +990,11 @@ namespace Vocaluxe.Lib.Draw t = new Texture(_Device, (int)texture.W2, (int)texture.H2, 0, Usage.AutoGenerateMipMap, Format.A8R8G8B8, Pool.Managed); //Lock the texture and fill it with the data DataRectangle rect = t.LockRectangle(0, LockFlags.Discard); - for (int i = 0; i < data.Length; i += 4 * w) + int rowWidth = 4 * (int)texture.W2; + for (int i = 0; i + rowWidth <= data.Length; i += 4 * w) { - rect.Data.Write(data, i, 4 * (int)texture.W2); - rect.Data.Position = rect.Data.Position - 4 * (int)texture.W2; + rect.Data.Write(data, i, rowWidth); + rect.Data.Position = rect.Data.Position - rowWidth; rect.Data.Position += rect.Pitch; } t.UnlockRectangle(0); diff --git a/Vocaluxe/Program.cs b/Vocaluxe/Program.cs index b912c774..bb715000 100644 --- a/Vocaluxe/Program.cs +++ b/Vocaluxe/Program.cs @@ -1,10 +1,30 @@ -using System; +#region license +// /* +// This file is part of Vocaluxe. +// +// Vocaluxe is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// Vocaluxe is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with Vocaluxe. If not, see . +// */ +#endregion + +using System; using System.Diagnostics; -using System.Drawing; using System.IO; +using System.Linq; using System.Reflection; using System.Windows.Forms; using Vocaluxe.Base; +using System.Runtime.ExceptionServices; namespace Vocaluxe { @@ -13,13 +33,33 @@ namespace Vocaluxe { private static CSplashScreen _SplashScreen; - [STAThread] -// ReSharper disable InconsistentNaming + [STAThread, HandleProcessCorruptedStateExceptions] + // ReSharper disable InconsistentNaming private static void Main(string[] args) -// ReSharper restore InconsistentNaming + // ReSharper restore InconsistentNaming { + AppDomain.CurrentDomain.UnhandledException += UnhandledExceptionHandler; AppDomain.CurrentDomain.AssemblyResolve += _AssemblyResolver; + try + { + _Run(args); + } + catch (Exception e) + { + string stackTrace = ""; + try + { + stackTrace = e.StackTrace; + } + catch {} + MessageBox.Show("Unhandled error: " + e.Message + stackTrace); + CLog.LogError("Unhandled error: " + e.Message + stackTrace); + } + _CloseProgram(); + } + private static void _Run(string[] args) + { // Close program if there is another instance running if (!_EnsureSingleInstance()) { @@ -169,17 +209,7 @@ namespace Vocaluxe // Start Main Loop _SplashScreen.Close(); - try - { - CDraw.MainLoop(); - } - catch (Exception e) - { - MessageBox.Show("Unhandled error: " + e.Message + e.StackTrace); - CLog.LogError("Unhandled error: " + e.Message + e.StackTrace); - } - - _CloseProgram(); + CDraw.MainLoop(); } private static void _CloseProgram() @@ -187,32 +217,68 @@ namespace Vocaluxe // Unloading try { + CLog.CloseAll(); CInput.Close(); CSound.RecordCloseAll(); CSound.CloseAllStreams(); CVideo.VdCloseAll(); CDraw.Unload(); - CLog.CloseAll(); CDataBase.CloseConnections(); CWebcam.Close(); } catch (Exception) {} } + [HandleProcessCorruptedStateExceptions] + private static void UnhandledExceptionHandler(object sender, UnhandledExceptionEventArgs args) + { + Exception e = (Exception)args.ExceptionObject; + string stackTrace = ""; + try + { + stackTrace = e.StackTrace; + } + catch {} + MessageBox.Show("Unhandled exception: " + e.Message + stackTrace); + CLog.LogError("Unhandled exception: " + e.Message + stackTrace); + } + private static Assembly _AssemblyResolver(Object sender, ResolveEventArgs args) { + // a fix to handle mscorlib.resources + if (args.Name.IndexOf(".resources", StringComparison.Ordinal) >= 0) + return null; + string[] arr = args.Name.Split(new char[] {','}); - if (arr != null) + if (arr.Length > 0) { + #if ARCH_X86 - Assembly assembly = Assembly.LoadFrom(Path.Combine("x86", arr[0] + ".dll")); + string path="x86"; #endif #if ARCH_X64 - Assembly assembly = Assembly.LoadFrom(Path.Combine("x64", arr[0] + ".dll")); + string path="x64"; #endif - - return assembly; + path = Path.Combine(path, arr[0] + ".dll"); + try + { + Assembly assembly; + try + { + assembly = Assembly.LoadFrom(path); + } + catch (FileLoadException) + { + //possibly loading from network, this would work always but better use the normal LoadFrom for more specific errors + assembly = Assembly.Load(File.ReadAllBytes(path)); + } + return assembly; + } + catch (Exception e) + { + CLog.LogError("Cannot load assembly "+args.Name+" from "+path+": "+e); + } } return null; } @@ -222,74 +288,7 @@ namespace Vocaluxe Process currentProcess = Process.GetCurrentProcess(); Process[] processes = Process.GetProcesses(); - if (processes == null) - return true; - - for (int i = 0; i < processes.Length; i++) - { - if (processes[i].Id != currentProcess.Id && currentProcess.ProcessName == processes[i].ProcessName) - return false; - } - - return true; - } - } - - class CSplashScreen : Form - { - private readonly Bitmap _Logo; - - public CSplashScreen() - { - string path = Path.Combine(Environment.CurrentDirectory, Path.Combine(CSettings.FolderGraphics, CSettings.Logo)); - if (File.Exists(path)) - { - try - { - _Logo = new Bitmap(path); - ClientSize = new Size(_Logo.Width, _Logo.Height); - } - catch (Exception e) - { - CLog.LogError("Error loading logo: " + e.Message); - } - } - else - CLog.LogError("Can't find " + path); - - path = Path.Combine(Environment.CurrentDirectory, CSettings.Icon); - if (File.Exists(path)) - { - try - { - Icon = new Icon(path); - } - catch (Exception e) - { - CLog.LogError("Error loading icon: " + e.Message); - } - } - else - CLog.LogError("Can't find " + path); - - SetStyle(ControlStyles.SupportsTransparentBackColor, true); - BackColor = Color.Transparent; - - FormBorderStyle = FormBorderStyle.None; - Text = CSettings.ProgramName; - CenterToScreen(); - Show(); - } - - protected override void OnPaint(PaintEventArgs e) {} - - protected override void OnPaintBackground(PaintEventArgs e) - { - if (_Logo == null) - return; - - Graphics g = e.Graphics; - g.DrawImage(_Logo, new Rectangle(0, 0, Width, Height)); + return processes.All(t => t.Id == currentProcess.Id || currentProcess.ProcessName != t.ProcessName); } } } \ No newline at end of file diff --git a/Vocaluxe/Screens/CScreenCredits.cs b/Vocaluxe/Screens/CScreenCredits.cs index bf985c56..10342170 100644 --- a/Vocaluxe/Screens/CScreenCredits.cs +++ b/Vocaluxe/Screens/CScreenCredits.cs @@ -28,8 +28,8 @@ class CCreditName _Image = image; _ImgDot = imgDot; _Particle = particle; - _ParticleOffsetX = (int)Math.Round(particleOffsetX * image.Rect.W / image.Texture.W2 - particle.Rect.W / 2); - _ParticleOffsetY = (int)Math.Round(particleOffsetY * image.Rect.H / image.Texture.H2 - particle.Rect.H / 2); + _ParticleOffsetX = (int)Math.Round(particleOffsetX * image.Rect.W / image.Texture.Width - particle.Rect.W / 2); + _ParticleOffsetY = (int)Math.Round(particleOffsetY * image.Rect.H / image.Texture.Height - particle.Rect.H / 2); Active = true; } diff --git a/Vocaluxe/Vocaluxe.csproj b/Vocaluxe/Vocaluxe.csproj index 2963f8a1..beac3ace 100644 --- a/Vocaluxe/Vocaluxe.csproj +++ b/Vocaluxe/Vocaluxe.csproj @@ -195,6 +195,9 @@ + + Form + @@ -205,10 +208,10 @@ Form - Form + @@ -236,9 +239,7 @@ - - Form - + @@ -277,16 +278,6 @@ Microsoft .NET Framework 4 Client Profile %28x86 und x64%29 und Update für .NET Framework 4 %28KB2468871%29 true - - False - .NET Framework 3.5 SP1 Client Profile - false - - - False - .NET Framework 3.5 SP1 - false - False Windows Installer 3.1