Skip to content

Commit

Permalink
Fixed armor display. Started on experimental server support.
Browse files Browse the repository at this point in the history
  • Loading branch information
mrkite committed Dec 19, 2011
1 parent d410a65 commit 1114b12
Show file tree
Hide file tree
Showing 11 changed files with 304 additions and 7 deletions.
13 changes: 13 additions & 0 deletions Terrafirma/ConnectToServer.xaml
@@ -0,0 +1,13 @@
<Window x:Class="Terrafirma.ConnectToServer"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Connect To Server" Height="186" Width="300">
<Grid>
<Label Content="Server IP" Height="28" HorizontalAlignment="Left" Margin="12,12,0,0" Name="label1" VerticalAlignment="Top" Width="76" />
<TextBox Height="23" HorizontalAlignment="Left" Margin="94,12,0,0" Name="serverip" VerticalAlignment="Top" Width="172" />
<Label Content="Server Port" Height="28" HorizontalAlignment="Left" Margin="12,46,0,0" Name="label2" VerticalAlignment="Top" Width="76" />
<TextBox Height="23" HorizontalAlignment="Left" Margin="94,46,0,0" Name="serverport" VerticalAlignment="Top" Width="69" Text="7777" />
<Button Content="Connect" Height="23" HorizontalAlignment="Right" Margin="0,112,12,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
<Button Content="Cancel" Height="23" HorizontalAlignment="Left" Margin="110,112,0,0" Name="button2" VerticalAlignment="Top" Width="75" Click="button2_Click" />
</Grid>
</Window>
46 changes: 46 additions & 0 deletions Terrafirma/ConnectToServer.xaml.cs
@@ -0,0 +1,46 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace Terrafirma
{
/// <summary>
/// Interaction logic for ConnectToServer.xaml
/// </summary>
public partial class ConnectToServer : Window
{
public ConnectToServer()
{
InitializeComponent();
}
public string ServerIP
{
get { return serverip.Text; }
}
public int ServerPort
{
get { return Convert.ToInt16(serverport.Text); }
}

private void button1_Click(object sender, RoutedEventArgs e) //connect
{
DialogResult = true;
this.Close();
}

private void button2_Click(object sender, RoutedEventArgs e) //cancel
{
DialogResult = false;
this.Close();
}
}
}
5 changes: 5 additions & 0 deletions Terrafirma/MainWindow.xaml
Expand Up @@ -13,6 +13,8 @@
</MenuItem>
<MenuItem Command="ApplicationCommands.Open" Header="_Open" />
<MenuItem Command="Refresh" />
<!-- <Separator />
<MenuItem Command="w:MapCommands.ConnectToServer" /> -->
<Separator />
<MenuItem Command="ApplicationCommands.Save" Header="_Save PNG" />
<Separator />
Expand Down Expand Up @@ -86,5 +88,8 @@
<CommandBinding Command="w:MapCommands.Wires"
Executed="Redraw"
CanExecute="TexturesUsed" />
<CommandBinding Command="w:MapCommands.ConnectToServer"
Executed="ConnectToServer_Executed"
CanExecute="Open_CanExecute" />
</Window.CommandBindings>
</Window>
168 changes: 168 additions & 0 deletions Terrafirma/MainWindow.xaml.cs
Expand Up @@ -44,6 +44,7 @@
using System.Windows.Interop;
using System.Collections;
using System.Threading;
using System.Net.Sockets;

namespace Terrafirma
{
Expand Down Expand Up @@ -251,6 +252,11 @@ public partial class MainWindow : Window
UInt32 skyColor, earthColor, rockColor, hellColor, lavaColor, waterColor;
bool isHilight = false;

Socket socket;
byte[] readBuffer,writeBuffer;
int pendingSize;
byte[] messages;

public MainWindow()
{
InitializeComponent();
Expand Down Expand Up @@ -1100,6 +1106,168 @@ private void Refresh_Executed(object sender, ExecutedRoutedEventArgs e)
load.Close();
}

private void ConnectToServer_Executed(object sender, ExecutedRoutedEventArgs e)
{
//we should disconnect if connected.

ConnectToServer c = new ConnectToServer();
if (c.ShowDialog() == true)
{
string serverip = c.ServerIP;
int port = c.ServerPort;
if (serverip == "")
{
MessageBox.Show("Invalid server address");
return;
}
if (port == 0)
{
MessageBox.Show("Invalid port");
return;
}

socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
System.Net.IPAddress ip;
try
{
ip = System.Net.IPAddress.Parse(serverip);
}
catch (Exception ex)
{
MessageBox.Show("Invalid server IP");
return;
}
System.Net.IPEndPoint remoteEP = new System.Net.IPEndPoint(ip, port);
socket.BeginConnect(remoteEP, new AsyncCallback(connected), null);
}
}
private void connected(IAsyncResult ar)
{
try
{
socket.EndConnect(ar);
//we connected, huzzah!
readBuffer = new byte[1024];
writeBuffer = new byte[1024];
messages = new byte[8192];
pendingSize = 0;
SendMessage(1); //greetings server!
socket.BeginReceive(readBuffer, 0, readBuffer.Length, SocketFlags.None,
new AsyncCallback(ReceivedData), null);
}
catch (Exception e)
{
socket.Close();
MessageBox.Show(e.Message);
}
}

private void ReceivedData(IAsyncResult ar)
{
try
{
int bytesRead = socket.EndReceive(ar);
if (bytesRead > 0)
{
Buffer.BlockCopy(readBuffer, 0, messages, pendingSize, bytesRead);
pendingSize += bytesRead;
messagePump();
socket.BeginReceive(readBuffer, 0, readBuffer.Length, SocketFlags.None,
new AsyncCallback(ReceivedData), null); //restart receive
}
else
{
// socket was closed?
}
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
}

private void messagePump()
{
if (pendingSize < 5) //haven't received enough data for even a message header
return;
int msgLen = BitConverter.ToInt32(messages, 0);
int ofs = 0;
while (ofs + 4 + msgLen <= pendingSize)
{
HandleMessage(ofs + 4, msgLen);
ofs += msgLen + 4;
if (ofs + 4 <= pendingSize)
msgLen = BitConverter.ToInt32(messages, ofs);
else
break;
}
if (ofs == pendingSize)
pendingSize = 0;
else if (ofs > 0)
{
Buffer.BlockCopy(messages, ofs, messages, 0, pendingSize - ofs);
pendingSize -= ofs;
}
}

private void HandleMessage(int start, int len)
{
int messageid = messages[start];
start++;
len--;
switch (messageid)
{
case 37: //request password.
ServerPassword s = new ServerPassword();
if (s.ShowDialog() == true)
SendMessage(38, s.Password);
else
socket.Close(); //cancelled? Then we're leaving the server.
break;
default:
MessageBox.Show(String.Format("Got response {0}", messageid));
break;
}
}

private void SendMessage(int messageid,string text=null)
{
int payload = 5;
int payloadLen = 0;
switch (messageid)
{
case 1: //send greeting
byte[] greeting = Encoding.ASCII.GetBytes("Terraria" + MapVersion);
payloadLen = greeting.Length;
Buffer.BlockCopy(greeting, 0, writeBuffer, payload, payloadLen);
break;
case 38: //send password
byte[] password = Encoding.ASCII.GetBytes(text);
payloadLen = password.Length;
Buffer.BlockCopy(password, 0, writeBuffer, payload, payloadLen);
break;
default:
throw new Exception(String.Format("Unknown messageid: {0}", messageid));
}

byte[] msgLen=BitConverter.GetBytes(payloadLen+1);
Buffer.BlockCopy(msgLen, 0, writeBuffer, 0, 4);
writeBuffer[4]=(byte)messageid;
socket.BeginSend(writeBuffer, 0, payloadLen + 5, SocketFlags.None,
new AsyncCallback(SentMessage), null);
}
private void SentMessage(IAsyncResult ar)
{
try
{
socket.EndSend(ar);
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
}

private void Hilight_Executed(object sender, ExecutedRoutedEventArgs e)
{
ArrayList items = tileInfos.Items();
Expand Down
2 changes: 2 additions & 0 deletions Terrafirma/MapCommands.cs
Expand Up @@ -60,5 +60,7 @@ static class MapCommands
"Show NPC Houses", "Houses", typeof(MapCommands));
public static readonly RoutedUICommand Wires = new RoutedUICommand(
"Show Wires", "Wires", typeof(MapCommands));
public static readonly RoutedUICommand ConnectToServer = new RoutedUICommand(
"Connect to Server...", "ConnectToServer", typeof(MapCommands));
}
}
4 changes: 2 additions & 2 deletions Terrafirma/Properties/AssemblyInfo.cs
Expand Up @@ -51,5 +51,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.9.2.0")]
[assembly: AssemblyFileVersion("1.9.2.0")]
[assembly: AssemblyVersion("1.9.3.0")]
[assembly: AssemblyFileVersion("1.9.3.0")]
12 changes: 8 additions & 4 deletions Terrafirma/Render.cs
Expand Up @@ -364,6 +364,7 @@ private struct Delayed

if (tiles[delay.offset].type == 128) //armor
{
int dy=8;
int au = tiles[delay.offset].u % 100;
int armor = tiles[delay.offset].u / 100;
switch (tiles[delay.offset].v)
Expand All @@ -372,24 +373,27 @@ private struct Delayed
tex = Textures.GetArmorHead(armor);
texw = 40;
texh = 36;
dy = 12;
break;
case 18: //body
tex = Textures.GetArmorBody(armor);
texw = 40;
texh = 54;
dy = 28;
break;
default: //legs
tex = Textures.GetArmorLegs(armor);
texw = 40;
texh = 54;
dy = 44;
break;
}
if (au >= 36) //reverse
drawTextureFlip(tex, texw, texh, tiles[delay.offset].v * tex.width * 4 + au * 4,
pixels, delay.px, delay.py - 8, width, height, scale / 16.0, lightR, lightG, lightB);
drawTexture(tex, texw, texh, 0,
pixels, delay.px-4, delay.py - dy, width, height, scale / 16.0, lightR, lightG, lightB);
else
drawTexture(tex, texw, texh, tiles[delay.offset].v * tex.width * 4 + au * 4,
pixels, delay.px, delay.py - 8, width, height, scale / 16.0, lightR, lightG, lightB);
drawTextureFlip(tex, texw, texh, 0,
pixels, delay.px-4, delay.py - dy, width, height, scale / 16.0, lightR, lightG, lightB);
}
else if (tiles[delay.offset].type == 5) //tree leaves
{
Expand Down
10 changes: 10 additions & 0 deletions Terrafirma/ServerPassword.xaml
@@ -0,0 +1,10 @@
<Window x:Class="Terrafirma.ServerPassword"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="This server requires a password" Height="140" Width="300">
<Grid>
<Label Content="Password:" Height="28" HorizontalAlignment="Left" Margin="12,12,0,0" Name="label1" VerticalAlignment="Top" Width="68" />
<PasswordBox Height="23" HorizontalAlignment="Left" Margin="86,17,0,0" Name="password" VerticalAlignment="Top" Width="180" />
<Button Content="Login" Height="23" HorizontalAlignment="Left" Margin="191,66,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
</Grid>
</Window>
35 changes: 35 additions & 0 deletions Terrafirma/ServerPassword.xaml.cs
@@ -0,0 +1,35 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace Terrafirma
{
/// <summary>
/// Interaction logic for ServerPassword.xaml
/// </summary>
public partial class ServerPassword : Window
{
public ServerPassword()
{
InitializeComponent();
}
public string Password
{
get { return password.Password; }
}

private void button1_Click(object sender, RoutedEventArgs e)
{
DialogResult = true;
}
}
}

0 comments on commit 1114b12

Please sign in to comment.